Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Excuse me, is that port taken?

by coec (Chaplain)
on Aug 17, 2002 at 14:53 UTC ( [id://190863]=perlquestion: print w/replies, xml ) Need Help??

coec has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

At work, we have a very brain dead system whereby our stores (each of which have a unique 4 digit numerical identifier) connect to port on the main AIX cluster for the purpose of getting the latest prices for trade customers. The silly thing about this system is that sockets are not correctly used and many ports are in use on the AIX cluster where, in a perfect world, only one port should be in use.

I have had to write control scripts(start, stop, monitor in ksh as my Perl is not quite up to the task as yet) for this system. The start script:
- starts the binary and tells it which unique port to listen on (50,000 + store number),
- sleep for 2 seconds (cos it fails otherwise), and
- do a 'ps -ef | grep ...' to see if the binary is still around (they have a habbit of dropping out).

I was thinking today that rather than doing a 'ps -ef | grep ...' it would be better to check and see if anything is listening on that port. There are generally 10,000+ processes at any one time so the ps/grep combination is very slow.

I have no idea how to get a Perl script to see if a port has a program bound to it. So a pointer in that direction would be great.

Thanks
CC

Replies are listed 'Best First'.
Re: Excuse me, is that port taken?
by clintp (Curate) on Aug 17, 2002 at 15:50 UTC
    Two ways spring to mind. First, connect to the port yourself to see if anything is listening (untested):
    use IO::Socket::INET; $sock=IO::Socket::INET->new(PeerAddr => 'machinename', PeerPort => $portnumber, Proto => 'tcp');
    And if you get a "connection refused" there's probably no listener.

    Another way is to bind the port yourself. If it's a low-numbered port, you'll have to be root.

    use IO::Socket::INET; $sock=IO::Socket::INET->new(Listen => 1, LocalAddr => $interfaceaddres, LocalPort => $portnumber, ReuseAddr => 1, Proto => 'tcp');
    If you don't get a "port already bound" error, then it's not bound.

    If either of these fail, $sock will be undef and $! will have your error message.

Re: Excuse me, is that port taken?
by no_slogan (Deacon) on Aug 17, 2002 at 17:31 UTC
    Try clintp's suggestion. Or try `netstat -an`, which gives you a list of what ports are being listened to. If you want to know what process is doing the listening, take a look at lsof.

    Is there some reason you can't record the binary's PID when you launch it ($! in ksh, or the return value from fork in perl, or look for a pid file somewhere), and then just check if that PID is still running (using ps with appropriate arguments, or kill 0=>$pid, or something)?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://190863]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-25 17:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found