Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Code Review: Checking open ports.

by Chady (Priest)
on Jun 09, 2003 at 08:12 UTC ( #264266=perlquestion: print w/replies, xml ) Need Help??
Chady has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I'm writing a small utility to monitor certain services on a dozen servers or so. I shouldn't have access on these servers, so I decided for the POP, SMTP and FTP monitoring to just check if their ports are open, since I wouldn't have access to check a logon.

I wrote this snippet:

sub checkports { my $sock; my ($pop, $smtp, $ftp) = @_; DEBUG && print "Checking ports : "; my %ports = ( 110 => $pop, 25 => $smtp, 21 => $ftp ); for my $port (keys %ports) { DEBUG && print "[$port] "; $sock = new IO::Socket::INET(PeerAddr => $ports{$port}, PeerPo +rt => $port, Proto => 'tcp', Timeout=>10); if ($sock) { $client->{"PORT$port"} = 'U'; close $sock; } else { $client->{"PORT$port"} = 'D'; } } DEBUG && print " done.\n"; }

Since I have a fairly small knowledge in networking, I was wondering if this approach is reasonable; Is there another way to check if the services are up without having to get a username/password and try a real login?


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re: Code Review: Checking open ports.
by ozone (Friar) on Jun 09, 2003 at 08:58 UTC

    The way this works, you're not really testing that the services actually are functioning. You're just connecting to the port, which is done at the OS level. So your service could've hung, but as far as you're concerned, it's still alive.

    Also, most services have a no-op type function that returns some sort of standard string you can test for. With SMTP, you could 'say hello' by sending 'HELO bob', and expect to get a response. I can't remember the other ones.

      Ozone is right, connecting to the port only verifies there is a program that is listening and bound to the port and acepting incoming connections. If your envornment normally sees crashes of those services where they server fine unless they are not running at all this may be "enough" for you. else more active tests are needed and you either need to understand the protocols that you are testing or use off the shelf modules to test functionality such as lwp for HTTP, net::ftp for FTP etc. note that this type of testing usually requires actuall access to the service (user for ftp, access to a web page to download). You need to decide what level of testing makes you comfortable.

      -Waswas
      With smtp, helo should elicit a 250 response code, with pop, user postmaster (or other valid login name) should result in a +OK response, and for ftp, a noop should return a 200 response code.

      ~~
      naChoZ

      You should be aware that at least sendmail servers logs connection that doesn't do anything useful, ie. only a connect and disconnect, or even a connect, HELO, NOOP and a disconnect. So you better get permission from the server administrator, or he might come back and ask you what you are doing and tell you to stop. It happend to me...
Re: Code Review: Checking open ports.
by hardburn (Abbot) on Jun 09, 2003 at 13:59 UTC

    Instead of making a new program, why not just use nmap (external link)?

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2018-11-13 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (158 votes). Check out past polls.

    Notices?