#!/usr/bin/env perl use warnings; use strict; # # Author: # # 2011(C) # # Change History # 0.1 Initial Release # 0.2 20150109.01 Added epoport.txt file to pass ePO port # # Description: Reads list of server names by their host names, checks to see if # they are alive via ping, completes an nbtstat -a and returns any lines containing # the host name and then checks to see if ePO port is open on the alive system. # # Input: # srvlst.txt containing one hostname per line # epoport.txt containing ePO port # # Output: # PingResults.csv # Format: Hostname, IP address, Alive status (Yes|No|Unknown Host|Unreachable|Unknown status), NBTSTAT result (NUL|$hostname), ePO port status (Open|closed) my $epoport = ''; my $socket = ""; my $hostcnt = 0; open (my $conf, ') { my $line = $_; chomp($line); ($epoport) = split(",", $line); }; close ($conf); print "ePO port: $epoport\n\n"; open (IN, "PingResults.csv") or die "Run aborted. Unable to open result file (PingResults.csv)"; print OUT "Hostname,IP,Alive,NBTHost,Port $epoport\n"; print "Start to check hosts\n"; while () { my $HOST = $_; chomp $HOST; print "Checking $HOST... "; my $IP = ''; my $NBTHost = ''; my $Alive = 'No'; my $ePOPort = 'Closed'; my $out = ""; ++$hostcnt; my $PingReply = `PING -n 1 -w 1 $HOST`; if ($PingReply =~ /unreachable/) { print "unreachable\n"; $Alive = 'Unreachable'; } elsif ($PingReply =~ /Reply from/) { $Alive = 'Yes'; print "alive "; my @Temp = split / /, $PingReply; $IP = $Temp[2]; $IP =~ s/\[//g; $IP =~ s/\]//g; my $NBTResults = `nbtstat -a $IP | find \"UNIQUE\"`; while (index($NBTResults,' ') > -1) { $NBTResults =~ s/ / /g; } my @NBTResults = split /\n/, $NBTResults; # We only need the first returned hostname of the NBT results (they're usually duplicates) my @LineTemp = split / /, $NBTResults[0]; $NBTHost = $LineTemp[1]; if ($NBTHost eq $HOST) { $NBTHost = ''; print "unique\n"; # Check ePO open port my $epoResponse = CheckPort($HOST,$epoport); if ($epoResponse == 1) { $ePOPort = "Open"; } else { $ePOPort = "Closed"; }; } else { print "Not unique\n"; $Alive = 'Unknown status'; } } elsif ($PingReply =~ /could not find host/) { print "unknown\n"; $Alive = 'Unknown host'; } else { print "not alive\n"; $Alive = 'No'; } print OUT "$HOST,$IP,$Alive,$NBTHost,$ePOPort\n"; } close OUT; close IN; print "\nCheck completed. Checked $hostcnt hosts\n"; sub CheckPort { use IO::Socket::INET; my ($host,$port) = @_; my $open = eval{ $socket = new IO::Socket::INET # opens a socket ( PeerAddr => $host, PeerPort => $port, Proto => getprotobyname('tcp') || 6 # tcp socket or sopcket 6(for old comps) )}; if ($open) { return 1; } else { return 0; } }