Closed ryandesign closed 2 years ago
On an Ubuntu 20 VM, I see the output from ss -4Htnl
looks like this:
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
This is presumably a list of all the listening sockets. The awk
command then looks at the 4th column and stores what's after the colon (the port) in the seen
array. At the END
, it starts from 1025 looking for a port number that was not recorded in the seen
array and prints the first found available port.
On macOS (I tested on 10.15) it looks like netstat -a -n -finet -ptcp
can provide similar information. Here's a subset of that output:
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 192.168.1.2.61098 17.57.144.89.5223 ESTABLISHED
tcp4 0 0 192.168.1.2.50207 192.168.1.103.49972 CLOSE_WAIT
tcp4 0 0 *.445 *.* LISTEN
tcp4 0 0 192.168.1.2.54690 192.168.1.8.58483 CLOSE_WAIT
tcp4 0 0 192.168.1.2.54438 192.168.1.103.51678 CLOSE_WAIT
tcp46 0 0 *.5055 *.* LISTEN
tcp4 0 0 *.5055 *.* LISTEN
It doesn't appear to have a way to output only listening sockets, but that's easy enough to achieve with post-processing.
The test suite says it passed on my Mac:
However, I'm not sure whether these results can be trusted since in each test's .err file it says:
This is of course coming from the preamble which says:
I wasn't familiar with
ss
but found the ss manpage and reading your code it looks like you're trying to use it to find a port that's not in use which the test copy of nginx will use. Is that right? Maybe there is a different command that can give us that information on macOS.It looks like since it could not run
ss
it used port 1025, which I think is free on my system, so maybe the test results are correct.