What's the easiest way to find an unused local port?
Currently I'm using something similar to this:
port=$RANDOM
quit=0
while [ "$quit" -ne 1 ]; do
netstat -a | grep $port >> /dev/null
if [ $? -gt 0 ]; then
quit=1
else
port=`expr $port + 1`
fi
done
It feels awfully roundabout, so I'm wondering if there's a more simple path such as a builtin that I've missed.


-nto netstat and a more selective grep). The way to do it is to try and open a port in whatever mode you need, and try another one if it's not available. – Mat Nov 16 '12 at 16:04ssh -Das a SOCKS server. – mybuddymichael Nov 16 '12 at 16:08