Thursday, February 8, 2007

Finding an unused port


#!/bin/sh
#To find an unused port

PORT=3306
quit=0

while [ "$quit" -ne 1 ]
do
netstat -a | grep $PORT >> /dev/null
if [ $? -ne 0 ]
then
quit=1
else
PORT=`expr $PORT + 1`
fi
done
echo "Free Port is $PORT"

3 comments:

(DISABLED) said...

I found this page googling for a quick example, and well, it's not a good example. Here's mine:

Port=5900
while netstat -atwn | grep "^.*:${Port}.*:\*\s*LISTEN\s*$"
do
Port=$(( ${Port} + 1 ))
done
echo "Looks like you get port ${Port}."

Good day.

Unknown said...

@Billy, thanks for the example, its useful.

Sesh said...

Thanks Billy!

© Jadu Saikia www.UNIXCL.com