Created
April 30, 2009 11:59
-
-
Save antonlindstrom/104423 to your computer and use it in GitHub Desktop.
School assignment #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# Pinging a server and checking if Network services are up or down. | |
use warnings; | |
use IO::Socket::INET; | |
# Network services to check (dns will not resolv). | |
@services = qw/http https ssh telnet mail dns mysql/; | |
# Variables, argument 0 (first one) and executing server in *nix env. | |
die("Usage: ./pinghost.pl [IP|Hostname]") unless ($ARGV[0]); | |
$server = $ARGV[0]; | |
# Open socket and check if it works | |
sub port_scan { | |
# Assign array to list. | |
($addr, $port, $protocol) = @_; | |
$portnum = getservbyname($port, $protocol); | |
# If service could not be resolved return 0 (exit subroutine). | |
return 0 unless($portnum); | |
# Open socket to $addr at $port on $protocol. | |
$sock = IO::Socket::INET->new(PeerAddr => $addr, | |
PeerPort => $portnum, | |
Proto => $protocol, | |
Timeout => 2 | |
); | |
# Port up or down. | |
print "$port ($portnum) is up.\n" if ($sock); | |
print "$port ($portnum) is down.\n" if (!$sock); | |
} | |
# Resolve FQDN. | |
$hostname = gethostbyaddr(inet_aton($server), AF_INET); | |
# Print FQDN or errormsg. | |
print("$server FQDN is $hostname.\n") if($hostname); | |
print("FQDN for $server could not be resolved.\n") unless($hostname); | |
# Hold your horses, pinging server and sending output to /dev/null | |
system("ping -c 3 $server > /dev/null"); | |
# If ping was successfull (returned 0) it is reachable. | |
if ( $? == 0 ) { | |
print "$server is reachable.\n"; | |
} else { | |
print "$server is unreachable.\n"; | |
# Exit if server is unreachable. | |
exit 0; | |
} | |
# Run subroutine port_scan to find out if service is up or down. | |
foreach $service(@services) { | |
&port_scan($server, $service, "tcp"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment