Posted: Fri Dec 20, 2002 7:39 pm Post subject: Script for testing status of a service
I made this script for a friend, just thought i would post it incase anyone else needed something similar.
Basically, its a perl script that opens a connection to a pre-defined IP address and port. It can then return the status of that port, either up or down.
This is useful if you run a network, and want to check the status of certain daemons running on specific machines.
Quote:
#!/usr/bin/perl -w
#
# porttest.pl
#
# Perl Script to test connection to ports.
# Used for checking service / server availability
# we want to use sockets
use IO::Socket;
# test that 2 parameters have been passed to script. If not, quit
$ARGC=@ARGV;
if ($ARGC !=2) {
print "Usage: perl porttest.pl <hostIPaddress> <portnumber> \n";
exit;
}
# assign cmdline values to vars
$remo = $ARGV[0];
$openport = $ARGV[1];
# Test status, if unable to connect, quit with error msg DOWN
unless ($so = IO::Socket::INET->new (Proto => "TCP", PeerAddr => $remo,
PeerPort => $openport))
{
print "DOWN\n";
exit;
}
# Script got to this point, therefore not down, so write UP, and exit
print "UP\n";
close $so;
exit;
Here is the source of a php page that calls the script (by "Anigel", www.star-fury.com):
Seems like alot of typing for ps -ax ? Or am I missing the point?
Correct.
The point is that this can test the status of a service on another machine. It is better than pinging, esp if ICMP is blocked by a firewall. Also, just becuase the machine is up, it does not mean a particular service on a particular port is working..
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum