Script for testing status of a service

Networking/Security Forums -> Programming and More

Author: Jason PostPosted: 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):

Quote:

<?
include ("../common.php");
include ("style2.php");
?>
<font size=+2>
<center>
<h3>Star-Fury Network Status</h3>
<P>
<pre>
<h3>Merlin</h3>
IRC <?system("/usr/local/sbin/porttest.pl merlin.star-fury.com 6667")?>
WWW <?system("/usr/local/sbin/porttest.pl merlin.star-fury.com 80")?>

<h3>Eclipse</h3>
IRC <?system("/usr/local/sbin/porttest.pl 127.0.0.1 6667")?>
WWW <?system("/usr/local/sbin/porttest.pl 127.0.0.1 80")?>

<h3>Trinity</h3>
WWW <?system("/usr/local/sbin/porttest.pl trinity.star-fury.com 80")?>

<h3>Oblivion</h3>
IRC <?system("/usr/local/sbin/porttest.pl oblivion.star-fury.com 6667")?>

</pre>
</font>
Thanks to <a href=http://www.jason-lambert.com>Jason Lambert</a> for theback end script<P>

<?
include("footer.php");


The direct link to a working example is here:
http://eclipse.star-fury.com/main/porttest.php

J


Last edited by Jason on Sat Dec 23, 2006 6:31 pm; edited 3 times in total

Author: Anigel PostPosted: Fri Dec 20, 2002 7:47 pm    Post subject:
    ----
Just for your information.

The up and down returns were modified to provide a direct link to the image in the above example

eg

# Test status, if unable to connect, quit with error msg DOWN
unless ($so = IO::Socket::INET->new (Proto => "TCP", PeerAddr => $remo,
PeerPort => $openport))
{
print "<img src=down.gif>\n";
exit;
}

# Script got to this point, therefore not down, so write UP, and exit
print "<img src=up.gif>\n";
close $so;
exit;

Author: flwLocation: U.S.A. PostPosted: Fri Dec 27, 2002 9:52 pm    Post subject:
    ----
Seems like alot of typing for ps -ax ? Or am I missing the point?

Author: Jason PostPosted: Sat Dec 28, 2002 8:49 am    Post subject:
    ----
fastlanwan wrote:
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.. Smile



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group