Tuesday, February 17, 2009

Basic PERL: Ckecking open ports/running services

Checking open ports or enable service, this could be done by using this simple Perl script. A very simple tool that uses IO module particularly socket and not to load other unnecessary modules.I have this script the checks to see if ssh service is up and running or blocked and possibly offline before doing any file transfer via scp.
This can also be used to scan your server for open ports and any running service that you may not be aware off. Customizing this script to actually scan a wide range of host name as well as specific port range by just adding nested loops against array of host names and ports. This would not in any way verify if the service is actually doing it's job or if it's having some problem except if it has inaccessible port.


#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket::INET;

sub main {

chkRemoteHost("my.server.name","22");

}


sub chkRemoteHost{
my $RemoteHost = $_[0];
my $RemotePort = $_[1];
my $socket = new IO::Socket::INET (
PeerAddr => $RemoteHost,
PeerPort => $RemotePort,
Proto => 'tcp',
timeout => "5"
);
if ($socket)
{
print ("Service ($RemoteHost:$RemotePort) is up and running n ");
close($socket);

}
else
{
printf ("Service ($RemoteHost:$RemotePort) is offline n ");

}
}
&main();