LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-03-2011, 12:37 PM   #1
laredotornado
Member
 
Registered: Apr 2011
Posts: 71

Rep: Reputation: 0
One line way to find an open port?


Hi,

I'm using Ubuntu Linux 11.04. Is there a way to find an unused port, starting from 4444 and gradually going upwards? I'm grateful for brevity on this one, since I'm using this port value in a Perl script, so I would like to call ...

my $openPort=`my_port_finding_command`;

Thanks, - Dave
 
Old 08-03-2011, 12:55 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
I would guess the best way would be to use awk to parse netstat output.
 
Old 08-04-2011, 04:21 PM   #3
kiran_cyberpro
LQ Newbie
 
Registered: May 2007
Distribution: Fedora
Posts: 11

Rep: Reputation: 3
Here is a quick pseudocode assume it is in php:
1.
$output = `netstat -l`; //execute netstat
2.
(filter lines containing only tcp/udp)
3.
then read this output line-by-line
4. per each line split or explode with the delimiter may be space/tab whatever.
5. parse the Nth item which is port and check it falls within the range.


Tip: you can use also a combination of sed, cut,echo statements with the first netstat -l via which you can greatly filter-out already what really you need. This reduces the parsing overheads.

For example netstat with sed: (multiple spaces are quickly converted to single spaces)
[root@koi ~]# netstat -l | sed 's/ / /g' | sed 's/ / /g' | sed 's/ / /g'| sed 's/ / /g'
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:mysql *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:35604 *:* LISTEN
tcp 0 0 192.168.122.1:domain *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 koi:smtp *:* LISTEN
tcp 0 0 *:55011 *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:http *:* LISTEN
tcp 0 0 *:8181 *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:https *:* LISTEN
udp 0 0 *:34292 *:*
udp 0 0 192.168.122.1:domain *:*
udp 0 0 *:bootps *:*
udp 0 0 *:sunrpc *:*
udp 0 0 *:ipp *:*
udp 0 0 115.184.76.140:ntp *:*
udp 0 0 192.168.122.1:ntp *:*
udp 0 0 koi:ntp *:*
udp 0 0 *:ntp *:*


Now with Sed and cut: (capture only the port numbers)
[root@koi ~]# netstat -l | sed 's/ / /g' | sed 's/ / /g' | sed 's/ / /g'| sed 's/ / /g' | cut -d ":" -f 2 | cut -d " " -f 1
Active
Proto
mysql
sunrpc
35604
domain
ssh
smtp
55011
sunrpc
http
8181
ssh
https
34292
domain
bootps
sunrpc
ipp
ntp
ntp
ntp
ntp
mdns
ns
783
52025
864
sunrpc
44667
ntp
ntp

Last edited by kiran_cyberpro; 08-04-2011 at 04:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help scripting to find line and print previous line to out jamieofansa Programming 4 05-21-2010 01:30 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
TCP port 708 open - how to find out by which program? Yalla-One Slackware 2 03-25-2006 10:12 AM
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM
Can not find why port 1720 is open grizzly Linux - Security 3 01-09-2004 12:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration