Search for the next open port

Is there a way, using basic Unix commands, to find the next unused port number, starting at port 4444 and going up? I ssh'ed (via openssh) into a Windows XP machine, run Cygwin tools and use the bash shell.

Thanks - Dave

+4
source share
3 answers

Try the following:

for port in $(seq 4444 65000); do echo -ne "\035" | telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "unused $port" && break; done 

Where

 seq 4444 65000 - port range for check echo -ne "\035" - escape character to force close telnet session (^]) 

if telnet ends with exit code 1, which means a connection failure:

 $ telnet 127.0.0.1 4444 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host $ echo $? 1 

else we decided that the connection was successful with exit code 0.

EDIT: Especially for cygwin: you need to install an additional inetutils package that contains a telnet port and use the script as follows:

 for port in $(seq 4444 65000); do echo -ne "\035" | /usr/bin/telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "unused $port" && break; done 
+9
source

Same as above, but written as a function

 function get_unused_port() { for port in $(seq 4444 65000); do echo -ne "\035" | telnet 127.0.0.1 $port > /dev/null 2>&1; [ $? -eq 1 ] && echo "$port" && break; done } FREE_PORT="$(get_unused_port)" echo $FREE_PORT 
+2
source

The following function is independent of telnet / netcat, since it generates a random port in the local port range and compares it with the list of ports currently used when starting applications.

Should work on any * nix that supports the proc file system. Creates a free ephemeral port that will be used by your application.

 function EPHEMERAL_PORT(){ while true; do LISTENING_PORTS=$(cat /proc/net/tcp | awk 'NR >1 {print $2}' | awk -F':' '{print $2}'); LISTENING_PORTS=$(for PORT in ${LISTENING_PORTS}; do echo $((16#${PORT})); done|sort -g); # echo "32768 60999" | read LPORT UPORT read LPORT UPORT < /proc/sys/net/ipv4/ip_local_port_range MPORT=$[$LPORT + ($RANDOM % $UPORT)]; if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then echo $MPORT; break; fi done } 

Apparently TCP connections can be used as linux file descriptors . The next function uses this technique and should be faster than the previous one.

 function EPHYMERAL_PORT(){ LPORT=32768; UPORT=60999; while true; do MPORT=$[$LPORT + ($RANDOM % $UPORT)]; (echo "" >/dev/tcp/127.0.0.1/${MPORT}) >/dev/null 2>&1 if [ $? -ne 0 ]; then echo $MPORT; return 0; fi done } 

This is a cross-platform feature that uses osquery to get a list of listening ports. You should also work with Windows.

 function EPHYMERAL_PORT(){ while true; do echo "32768 60999" | read LPORT UPORT MPORT=$[$LPORT + ($RANDOM % $UPORT)]; LISTENING_PORTS=$(osqueryi --header=false --list "select port from listening_ports order by port"); if (echo "${LISTENING_PORTS[@]}" | grep -xqv $MPORT); then echo $MPORT; break; fi done } 

Instructions for use. Bind the output to a variable and use it in scripts. Tested on Ubuntu 16.04

 root@ubuntu :~> EPHYMERAL_PORT 59453 root@ubuntu :~> PORT=$(EPHYMERAL_PORT) 
+2
source

All Articles