#!/usr/bin/perl use strict; use Net::Ping; # name: PINGuin # author: ap0calypse (ap0calypse@agitatio.org) # purpose: does a tcp-ping on a given range of hosts. # # license: DO WHATEVER YOU WANT! # # usage: ./pinguin [-d] xx.xx.xx.* # # version: 0.1b (2008-05-05) # 0.1 (2008-05-06) # # requires: Net::Ping # host (system command) # the 'host' command is only neccessary if you use -d # add yourself to the authors if you change the source my $VERSION = "0.1"; my @AUTHORS = ( "ap0calypse (ap0calypse\@agitatio.org)", ); # the usage-output sub usage { print << "EOUSAGE"; pinguin ($VERSION) Usage: pinguin [-d] xx.xx.xx.* pinguin tries to do a TCP-Ping to a given range of hosts and reports if the hosts are responding. you can use wildcards (*) to specify the range. some examples: pinguin -d 192.168.0.* -> ping 192.168.0.1 to 192.168.0.254 plus DNS pinguin 192.168.0.1 -> ping 192.168.0.1 pinguin 192.168.0.11* -> ping 192.168.0.110 to 192.168.0.119 pinguin -d 192.168.0.3* -> ping 192.168.0.30 to 192.168.0.39 plus DNS pinguin 192.168.0.2* -> ping 192.168.0.20 to 192.168.0.29 and 192.168.0.200 to 192.168.0.254 Bugs: there are no bugs, only features ;). this piece of crap comes with absolutely NO WARRANTY! if this script kills your dog or steals your wife, it's not my fault! EOUSAGE exit 1; } # check the input &usage if (@ARGV == 0 or @ARGV > 2); # default is no DNS-Lookup (0) my $do_dns; # do we want name-lookup ? if (scalar @ARGV == 2) { if (shift @ARGV eq "-d") { $do_dns = 1; } else { &usage; } } my $ping_object = Net::Ping->new("tcp"); # $input is the ip-range my $input = shift; # detect if the input is a valid ip-adress if ($input =~ m/\A (?:[01]?\d\d?|2[0-4]\d|25[0-5])\. (?:[01]?\d\d?|2[0-4]\d|25[0-5])\. (?:[01]?\d\d?|2[0-4]\d|25[0-5])\. (.+) \z/mx ) { # the last octet ($1) is changeable my $varpart = $1; # the first 3 octets are static in this version my $threeoctets = join ".", (split /\./ ,$input)[0 .. 2]; my @range; my $fix; if ($varpart =~ m/\A\*\z/) { @range = "1" .. "254"; } elsif ($varpart =~ m/\A(\d{1,2})\*\z/) { $fix = $1; if ( length $fix == 1) { if ($fix == 1) { @range = "0".. "9"; push @range, "00" .. "99"; } elsif ($fix == 2) { @range = "0" .. "9"; push @range, "00" .. "54"; } else { @range = "0" .. "9"; } } elsif (length $fix == 2) { if ($fix == 25) { @range = "1" .. "4"; } elsif ($fix > 25) { @range = (); } else { @range = "0" .. "9"; } } } elsif ($varpart =~ m/\A([01]?\d\d?|2[0-4]\d|25[0-5])\z/mx) { @range = (); $fix = $1; } else { &usage; } if (scalar @range == 0) { my $hostname = "$threeoctets.$fix"; print "pinging: \e[0;36m$threeoctets.$fix\e[0m\t"; if ($ping_object->ping( $hostname, 0.5 )) { print "[ \e[0;32mOK\e[0m ]"; } else { print "[ \e[0;31mER\e[0m ]"; } if ($do_dns) { chomp (my $hostout = `host $hostname`); my $dns = (split (/ /, $hostout))[-1]; $dns =~ s/(.+)\./$1/; print "\t$dns" if $dns !~ m/3\(NXDOMAIN\)/; } print "\n"; } elsif (scalar @range > 0) { for my $i (@range) { my $hostname = "$threeoctets.$fix" . "$i"; print "pinging: \e[0;36m$threeoctets.$fix$i\e[0m\t"; if ($ping_object->ping( $hostname, 0.5 )) { print "[ \e[0;32mOK\e[0m ]"; } else { print "[ \e[0;31mER\e[0m ]"; } if ($do_dns) { chomp (my $hostout = `host $hostname`); my $dns = (split (/ /, $hostout))[-1]; $dns =~ s/(.+)\./$1/; print "\t$dns" if $dns !~ m/3\(NXDOMAIN\)/; } print "\n"; } } } else { &usage; }