Sample Header Ad - 728x90

What's the easiest way to find an unused local port?

106 votes
20 answers
139792 views
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.
Asked by mybuddymichael (1215 rep)
Nov 16, 2012, 03:58 PM
Last activity: Sep 30, 2024, 06:03 AM