Hello again,
Today's entry harkens back to a post we did a while ago regarding checking your network status using CGI. Although the Perl script we're going to run through today is somewhat similar, there are more than a few notable differences.
This Perl script should be able to be run on pretty much any flavor of Linux or Unix that supports Perl and simple Berkeley Sockets. It's also meant to be run out of cron, so that you can keep Web Server error reports (or, hopefully, a lack thereof) coming on a dependable basis. This script doesn't rely on anything in the *nix "environment" that should make it fail, so a simple entry in your crontab, like:
0,30 * * * * /wherever/you/keep/this/wsMonitor.pl
should check your Web Server every 30 minutes.
Another way you can do this (which is explored in more detail in our previous post on alternatives to using the nohup command) is to run the command (either from an init script or straight from the command line) in a backgrounded subshell, with slight wrapper script around the code, like so:
---> Sample Wrapper Shell Code <---#!/bin/sh
while :
do
/wherever/you/keep/this/wsMonitor.pl
sleep 1800
done
---> End Sample Wrapper Shell Code <---
and then kick it off with a command line similar to this (again, either from an init script or directly from your shell prompt):
host # (/full/path/to/your/wrapper_script.sh &)
Okay, now back to the Perl script itself ;) You'll note that it works very simply by sending a string (whatever you want it to be - Maybe something meaningful, like "This is just us testing," to make it easy to keep out of your Web Server access reporting) to your Web Server and sending out an email if the Web Server doesn't respond. Note that this Perl script doesn't care what kind of response it gets, since it's not sending a valid HTTP request; it only checks to make sure that the Web Server is receiving the same amount of characters we send it in our test string. It will also send a slightly different mail message if it can't open the port at all.
I've commented on each line for all the stuff you might need (or want) to modify. Hopefully this script finds you, and your Web Server, well :)
Best wishes,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/usr/bin/perl
# wsMonitor.pl - Make Sure Our Web Server Is Answering On Port 80
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
use Socket;
require 5.002;
$mailer = "/usr/lib/sendmail"; # Your path to your local mail program
$remote = "webserver.xyz.com"; # Whatever host you want to check
$port = 80; # You can also check any other port, since we aren't looking for a particular response
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
&mail_out unless $port;
$iaddr = inet_aton($remote);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(BINGER, PF_INET, SOCK_STREAM, $proto) or &fail_mail;
connect(BINGER, $paddr) or &fail_mail;
$hey = send(BINGER, "TESTING TESTING 1 2 3", 21) or &fail_mail; # Note: Change the number after your message to the amount of characters being sent in between the quotes preceding it.
# In this case "TESTING TESTING 1 2 3" comes out to 21 characters, including the whitespace
exit;
sub fail_mail {
open (MAIL, "| $mailer -t") || die "Can't Open $mailer: $!\n";
print MAIL "To: interested_party\@xyz.com\n";
print MAIL "From: socketWatch\@monitorhost\n";
print MAIL "Subject: Possible problem with $remote!!!\n";
print MAIL "------------------------\n";
print MAIL "$remote incomplete Socket transmission:\n";
print MAIL "Returned $hey from 21...\n"; # Note that this 21 is the same as the 21 referenced in the socket send command above
close (MAIL);
exit;
}
sub mail_out {
open (MAIL, "| $mailer -t") || die "Can't Open $mailer: $!\n";
print MAIL "To: interested_parties\@pager.xyz.com\n";
print MAIL "From: socketWatch\@$remote\n";
print MAIL "Subject: Possible problem with $remote!!!\n";
print MAIL "------------------------\n";
print MAIL "$remote may be down: $!\n";
close (MAIL);
exit;
}
, Mike
linux unix internet technology
Friday, March 21, 2008
Checking Whether Your Web Server Is Up Using Perl Sockets Programming
Wednesday, January 2, 2008
A Simple CGI Script To Check Network Hosts' Status
Today, I thought I'd put out another script that might be helpful to at least a few of us out there. As you may recall, in an earlier post we went over a script that checked network hosts' ports non-maliciously. This is a combination of that with a little something extra.
Okay, there are a few extra elements here. The main function of the script is to check that all the servers in the @hostnames array are up and running. This is done as simply as possible, using ping. The second thing it checks is that FTP is up and running on all hosts (We're assuming that you're running this from a box on your network that has access - through firewalls, router acl's and whatnot - to all of the servers listed). Thirdly, it looks at a subset of servers that you define within the script, and tries to determine if HTTP is up and running.
This script is slightly more complicated than the other, as it uses the Perl Socket and CGI modules and also makes use of signal handling (Touched on in more detail in this previous post. Finally, we do some while loop tagging to control the flow.
Of course, this script can be modified easily to include other system checks. I wrote this to get a quick feel for the health of the network and get an easy read on any problems at whatever time I chose to open the page in my web browser.
Hopefully, you'll find this useful. I've commented the script near all the parts that can/should be edited (IN CAPITALS FOR EASE OF VIEWING). Otherwise, it's pretty much self explanatory.
Cheers,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/usr/bin/perl
#
# whatup.cgi - 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
use CGI;
use Socket;
require 5.002;
# FILL THIS ARRAY WITH ALL THE HOSTNAMES YOU WANT TO MONITOR
@hostnames = qw(host1 host2 host3 hosta hostb hostc hostn);
foreach $pingable_host (@hostnames) {
$answer=`/usr/sbin/ping $pingable_host 1 1`;
if ( $answer=~ /alive/ ) {
$is_it_up_or_not[$a] = 1;
$a++;
} else {
$is_it_up_or_not[$a] = 0;
$a++;
}
}
use CGI;
$whatup_cgi = new CGI;
@ports = qw(21 80);
$date = `date`;
print $whatup_cgi->header();
print "<html><head><meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"300; URL=/whatup.cgi\"><title>What's Up?</title></head>\n";
print "<body BGCOLOR=\"#000000\" TEXT=\"#00FF00\" LINK=\"#FFFF00\" VLINK=\"#FF0000\" ALINK=\"#BECD8C\">\n";
print "<p>\n";
print "<br><hr><br>\n";
print "<p>\n";
print "<center><FONT COLOR=\"#FF0000\"><H3>Your Immediate Guide to What's Up On Our Network!</h3></FONT></center>\n";
print "<center><FONT COLOR=\"#BECD8C\"><H4>$date</h4></FONT></center>\n";
print "<p>\n";
print "<br><hr><br>\n";
print "<p>\n";
$b=0;
$c=0;
$SIG{'ALRM'} = 'next_goround';
OUTER: foreach $host (@hostnames) {
if ( $is_it_up_or_not[$b] ) {
print "<hr>\n";
print "<h3><font color=\"#BECD8C\">$host is Up!</font></h3>";
} else {
print "<hr>\n";
print "<h3><font color=\"#FF0000\">$host is Down!</font></h3>";
}
print "<ul>";
$CONNECTION_PORT = "NEXT_UP_IS${c}SOCK";
# IN THE BELOW IF STATEMENT, USE REGULAR EXPRESSIONS TO MATCH ALL HOST NAMES
# THAT YOU WANT TO BE CHECKED FOR HTTP. ALL HOSTS ARE CHECKED FOR FTP
if ( $host=~ /host[1-3]/ || $host=~ /host[ab]/ ) {
foreach $portno (@ports) {
$port = $portno;
if ($port =~ /\D/) {
$port = getservbyname($port, 'tcp');
}
$iaddr = inet_aton($host);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket($CONNECTION_PORT, PF_INET, SOCK_STREAM, $proto);
if ( $is_it_up_or_not[$b] ) {
connect($CONNECTION_PORT, $paddr) unless alarm 10;
if ( $success_or_failure ) {
next;
}
alarm 0;
}
if ($port == 21 && ! $success_or_failure ) {
$portname = "FTP";
if ( $is_it_up_or_not[$b] ) {
$create_socket = send($CONNECTION_PORT, "user anonymous username\@xyz.com", 40) unless alarm 10;
alarm 0;
}
} else {
$portname = "HTTP";
if ( $is_it_up_or_not[$b] ) {
$create_socket = send($CONNECTION_PORT, "GET /index.html HTTP/1.0", 40) unless alarm 10;
alarm 0;
}
}
close($CONNECTION_PORT);
if ( ! $is_it_up_or_not[$b] && ! $success_or_failure ) {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font></ul>";
$b++;
next OUTER;
}
if ($create_socket) {
print "<li><h3><font color=\#FFFF00\">$portname is Up!</h3></font>";
} else {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font>";
}
$c++;
$success_or_failure = 0;
}
print "</ul>\n";
} else {
$port = 21;
if ($port =~ /\D/) {
$port = getservbyname($port, 'tcp');
}
$iaddr = inet_aton($host);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket($CONNECTION_PORT, PF_INET, SOCK_STREAM, $proto);
if ( $is_it_up_or_not[$b] ) {
connect($CONNECTION_PORT, $paddr) unless alarm 10;
alarm 0;
}
if ( $is_it_up_or_not[$b] && ! $success_or_failure ) {
$create_socket = send($CONNECTION_PORT, "user anonymous username\@xyz.com", 40) unless alarm 10;
alarm 0;
}
$portname = "FTP";
close($CONNECTION_PORT);
if ( ! $is_it_up_or_not[$b] && ! $success_or_failure ) {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font></ul>";
$b++;
next OUTER;
}
if ($create_socket) {
print "<li><h3><FONT COLOR=\"#FFFF00\">$portname is Up!</h3></font>";
} else {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font>";
}
print "</ul>\n";
$c++;
$success_or_failure = 0;
}
$b++;
}
$whatup_cgi->end_html();
sub next_goround {
close($CONNECTION_PORT);
return $success_or_failure = 1;
}
, Mike
linux unix internet technology
Wednesday, December 19, 2007
Non-Maliciously Scanning For Open Network Ports
Generally, port scanning is looked at as a "Black Hat" activity. This isn't so much stereotyping, as a heavily weighed and considered generalization. Entire industries are built around the fact that most, if not all, networked servers (public and private) are under almost constant attack by some individual or group. It's for that reason that most folks, like myself, take pause before ever engaging in such activity, even if our motives are pure. Depending on your situation, the very fact that you do this might get you flagged.
On the flipside, however, is the obvious "need" to be able to check whether ports are open. Reasons range from making sure that services are up and available, to "White Hat" activity, like making sure that all of the network ports that you think are closed actually are; thus enabling you to detect security risks and eliminate them before someone else can take advantage.
The script I'm putting up today is a port scanner in its purest sense. It doesn't attempt to "investigate" the services running on any of the network ports it finds open, which is usually enough to keep you from getting threatening emails from your security department ;) The little reporting it does do on open network ports is variable, as it gets the information from the /etc/services file on the host from which it's being run (using the standard getservbyport call).
You call the script by whatever name you give it (I'll call it "scanner" because it makes sense and, maybe, I'm not feeling very original right now ;) and supply it with the IP Address of the target server and the highest port on it that you want to scan. The script assumes you want to start at port 1. You can change that very easily by modifying the $port variable. You also need to be root to run this script, as it creates sockets (using standard Berkeley Sockets code) and regular users don't have system permission to do that on Unix and Linux systems. If they do, something is horribly wrong :P
A sample run would look like this:
host # ./scanner 127.0.0.1 1000
Open : 80/tcp :
Open : 111/tcp : sunrpc
Open : 443/tcp :
host #
In the above example, it appears that we've got a regular and secure webserver running, as well as the sunrpc service. The reason that the description field on the left is empty for ports 80 and 443 is that there are no entries in the host's /etc/services file for those ports. Simply adding those entries in that file would make the program produce this on an exactly equal run:
host # ./scanner 127.0.0.1 1000
Open : 80/tcp : http
Open : 111/tcp : sunrpc
Open : 443/tcp : https
host #
Hopefully, you'll find this useful with regards to your completely-above-board administration activities :)
Cheers,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/bin/perl
#
# scanner - simple port scanner
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
if ( $#ARGV != 1 ) {
print "Usage: $0 hostnameOrIP highPort\n";
exit(1);
}
use Socket;
$host = $ARGV[0];
$highport = $ARGV[1];
$port = 1;
OUTERLOOP: while ( $port <= $highport ) {
$| = 1;
$line = " ";
$tcp = 1;
INNERLOOP: while ($tcp >= 0) {
if ( $tcp ) {
$protocol = "tcp";
} else {
$protocol = "udp";
}
#print "${port}/$protocol... ";
if ( $port =~ /\D/) {
$port = getservbyname($port, $protocol);
}
unless ( $port ) {
print "No port : ${port}/$protocol\n";
if ( $tcp ) {
$tcp--;
next INNERLOOP;
} else {
$port++;
next OUTERLOOP;
}
}
$inet_address = inet_aton($host) || die "No host: ${host}!";
$port_address = sockaddr_in($port, $inet_address);
$protocol_num = getprotobyname('$protocol');
socket(SOCKET, PF_INET, SOCK_STREAM, $protocol_num) || die "Socket: $!";
unless ( connect(SOCKET, $port_address) ) {
if ( $tcp ) {
$tcp--;
next INNERLOOP;
} else {
$port++;
$tcp--;
next OUTERLOOP;
}
}
if ( $tcp ) {
send(SOCKET, $line, 'SOCK_STREAM') == length($line) || die "Cannot Send Message!: $!\n";
} else {
send(SOCKET, $line, 'SOCK_DGRAM', $protocol) == length($line) || die "Cannot Send Message!: $!\n";
}
close(SOCKET) || die "Close $!";
$service = getservbyport($port, $protocol);
print "Open : ${port}/$protocol : $service\n";
$port++;
$tcp--;
}
}
, Mike
linux unix internet technology
Sunday, November 11, 2007
Checking If A Mail Server Is An Open Relay Using Perl and Berkeley Sockets!
SPAM is common enough nowadays that you can probably find some version of this basic check somewhere on the web (probably a lot of places), but our intent here is to take a look at what's behind the pretty looking web pages and/or software, used in determining whether or not the mail server you're runnning (or any mail server, for that matter) is an open relay.
Below is a little script I put together using Perl (and its Socket module - based on Berkeley Sockets) that will do this check for you. In a future post, we'll explore Berkeley Sockets programming at a more granular level (as it's far outside the scope of this simple blog post), but for now check out the script below:
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#!/bin/perl
#
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
use Socket;
$host = "whateverMailServerYouWantToCheck.xyz.com";
$relaydomain = "xyz.com";
$sender = "eggi@noOneWillEverPickThisDomain.com";
$adressee = "info@hopefullyThisMailServerIsNotInTheDomainWereTesting.zyx.com";
$protocol = "tcp";
$port = 25;
if ( $port =~ /\D/) {
$port = getservbyname($port, $protocol);
}
unless ( $port ) {
print "No port : ${port}/$protocol\n";
}
$inet_address = inet_aton($host) || die "No host: ${host}!";
$port_address = sockaddr_in($port, $inet_address);
$protocol_num = getprotobyname('$protocol');
socket(SOCKET, PF_INET, SOCK_STREAM, $protocol_num) || die "Socket: $!";
connect(SOCKET, $port_address) || die "Socket $!";
send(SOCKET, $line, 'SOCK_STREAM') == length($line) || die "Cannot Send Message!: $!\n";
print SOCKET "HELO $relaydomain\n";
print SOCKET "MAIL From: $sender\n";
print SOCKET "RCPT To: $adressee\n";
print SOCKET "DATA\n";
print SOCKET "Subject: If this didn't get rejected - you may be running an open relay!\n";
print SOCKET ".\n";
print SOCKET "quit\n";
close(SOCKET) || die "Close $!";
exit(0);
At a very basic level, there are just a few things (covered in the script above) that you need to do to quickly determine if a mail server is an open relay. First, make sure you're sending from a different domain than the mail server's domain. Second, connect to the mail server and attempt to send mail to another (different) domain. If this works, the mail server is acting as an open relay.
Of course, like I said, it might be easier to just go to any number of sites on the web and have that free service do the check for you. But, in any event, this way can be more fun and you can customize it to do whatever (hopefully, good and decent) things you want it to :)
, Mike
linux unix internet technology





