Popular posts

Pages

Monday, April 4, 2011

perl program to report port status

This program here checks and reports the status of ports on a windows machine. The objective was to check whether some application (web site) was running and hence using those ports. The logic is simple. The ports (actually, ip:port combination) which are needed to be monitored are listed in a file. This could have been kept inside the code, for better performance. But i wanted to keep the "logic" portion separate from "data" portion so data / logic can be edited independently without affecting other by mistake. And ofcourse it is easier for the user to open a file and edit the port file, rather than going through the code and getting in unimportant details. So the port status is available thru windows "netstat -an" command. The status of our ports is checked and the output is logged into a separate log file. This log file, by the way, is read by another software which then puts the data on display for the user. Here's the program. First section is the custom timestamp for log file. Then every port entry in our file (first foreach loop) is compared with every netstat o/p line (second foreach loop) and mark a flag if matched. Then get out of inner loop, check the flag value, and log the entry with a custom message. I guess thats about it. At some places, such as pattern matching, it can be done more efficiently (with less characters) but i like to keep it simple. i think some advanced users may be able to do it with lesser line of code too. Basically it is a straight forward program which produces desired results. For perl beginners it might prove to be helpful and can be used in other similar situations.

#
#
# Purpose: Script to report status of active ports on Windows machine
# Associated Files: 
# 1. portlist.txt - list of ports. File required in current directory.
# 2. log_status.txt - port status log. File is generated in current directory.
# Date: 29 March 2011 
# Author: Abhishek Danej
# Revisions:
#

use Strict;
use Time::Local;

##
## USER EDITABLE PARAMETERS
##
$portfile = "portlist4.txt"; #path to list of ports file
$logfile = "log_status.txt"; #path to log file
$true_msg = "Listening"; #success message in log file
$false_msg = "Not found"; #failure message in log file
##
## END
##

## NON USER EDITRABLE SECTION

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon = $mon+1;
foreach $val ($sec, $min, $hour, $mday, $mon) {
$val = "0" . $val if ($val < 10); 
}
$timestamp = ($year+1900) . "-" . $mon . "-" . $mday . "," . $hour . ":" .  $min . ":" . $sec;

#$timestamp = scalar(localtime());
$host = `hostname`;
chop ($host);

open (LOG, ">>$logfile") || die "Cannot open Log file";

#@portlist = ("4757","1111","1101","6000","7153","57153");
unless (open (PORTS, "<$portfile")) {
print LOG "$timestamp ERROR: Cannot open portlist.txt file, program quitting.\n";
die "Cannot open port file."; }
@portlist = <PORTS>;

open (FH, "netstat -an |");
@lines = <FH>;

foreach $ip_port (@portlist) {

$flag = 0;
$ip_port =~ s/\s*\b(\d+\.\d+\.\d+\.\d+):(\d+)\b.*\n*/$1:$2/;
next if ($ip_port !~ /:/);
print "Now checking: $ip_port";
# EACH LINE OF NETSTAT
foreach $line (@lines) {
if ($line =~ /$ip_port\b/ && $line =~ /LISTENING/ && $line =~ /\b0.0.0.0\b/) {
$flag = 1;
last; }
}
if ($flag == 1) {
print LOG "$timestamp,$host,$ip_port,$true_msg\n"; }
else {
print LOG "$timestamp,$host,$ip_port,$false_msg\n"; }

}

close (PORTS);
close (LOG);
# END

No comments:

Post a Comment