How to find an open port?

August 03rd, 2011 - 02:26 pm ET by laredotornado | Report spam
Hi,

I'm writing a bash script and in it, I'd like to save to a shell
variable the next available port, including and after port 4444. I'm
using Ubuntu Linux 11.04. Does anyone know how I can write a command
or series of commands to do this?

Thanks, - Dave
email Follow the discussionReplies 3 repliesReplies Make a reply

Similar topics

Replies

#1 The Natural Philosopher
August 03rd, 2011 - 02:57 pm ET | Report spam
wrote:
Hi,

I'm writing a bash script and in it, I'd like to save to a shell
variable the next available port, including and after port 4444. I'm
using Ubuntu Linux 11.04. Does anyone know how I can write a command
or series of commands to do this?

Thanks, - Dave



start with netstat -t
?
Replies Reply to this message
#2 Bill Marcum
August 03rd, 2011 - 03:07 pm ET | Report spam
On 2011-08-03, wrote:
Hi,

I'm writing a bash script and in it, I'd like to save to a shell
variable the next available port, including and after port 4444. I'm
using Ubuntu Linux 11.04. Does anyone know how I can write a command
or series of commands to do this?

Thanks, - Dave



Perhaps you could parse the output of netstat or lsof.


Inside every older person is a younger person wondering what the hell
happened.
Replies Reply to this message
#3 Anonymous Remailer (austria)
August 05th, 2011 - 11:42 am ET | Report spam
[l]:
l> I'm writing a bash script and in it, I'd like to save to a shell
l> variable the next available port, including and after port 4444.
l> I'm using Ubuntu Linux 11.04. Does anyone know how I can write
l> a command or series of commands to do this?

# show currently used TCP ports
</proc/net/tcp cut -f3 -s -d: |
sed 's@ .*@@;s@^@0x@' |
xargs -n 1 printf "%d" |
sort -nu

# show first available TCP port at or above 4444
</proc/net/tcp cut -f3 -s -d: |
sed 's@ .*@@;s@^@0x@' |
xargs -n 1 printf "%d" |
sort -nu |
awk -v P=4444 '
BEGIN{next_available=P}
($1 < P){next_available=P}
($1 == P){next_available=P+1;P=P+1}
END{print next_available}
'


Please remember that ports are being opened by applications and
daemons dynamically. By the time the above command sequence gives
you an answer, it might already be a wrong one.
email Follow the discussion Replies Reply to this message
Help Create a new topicReplies Make a reply
Search Make your own search