> I get IP network definition in format like this one 10.10.16.0/23 and need

> to calculate after counter additional networks.
 
> In the first step I need to validate the correctness of delivered IP 
> network definition.
> In the second step increment in valid steps additional networks.
 
> In the moment I play with decimal numbers, but it is complex and looks not

> so professional style to program.
> Has someone an idea how to do it in another strategy like hexadecimal?
 

The below script might have some of what you need especially the &range sub.
However, you'd still need to be able to calculate the "last" IP from your
CIDR notation.

-Pete


### ping.pl

use Net::Ping;
use Win32::Console;


if (defined(@ARGV))
{
        if ($ARGV[0] eq "-range")
        {
                # ping a range of addresses. Careful!
                $first=$ARGV[1];
                $last=$ARGV[2];
                &range($first,$last);
        }
        elsif ($ARGV[0] eq "-n")
        {
                # ping a host N times.
                $n=$ARGV[1];
                shift(@ARGV);
                shift(@ARGV);
                @[EMAIL PROTECTED];
        }
        elsif ($ARGV[0] eq "-last")
        {
                # ping whatever it was that you ping'd last time
                open(LAST,"ping-pl.last") || &noCfg;
                @hosts=<LAST>;
                close(LAST);
        }
        elsif ($ARGV[0] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)
        {
                # ping this IP address
                push(@hosts,$ARGV[0]);
        }
        else
        {
                # open a config file and ping whatever's listed in there
                open(CFG,$ARGV[0]) || &noCfg;
                @hosts=<CFG>;
                close(CFG);
        }
}
else
{
        &noCfg;
}




$cons = Win32::Console->new(STD_OUTPUT_HANDLE);
$good = chr($FG_LIGHTGREEN | $BG_BLACK) x 80;
$bad = chr($FG_LIGHTRED | $BG_BLACK) x 80;
$cons->Cls;

$p = Net::Ping->new("icmp");





foreach $host (@hosts)
{
        chomp($host);   
        my $times=1;    
        my ($x, $y) = $cons->Cursor();
        #print "DEBUG: x=$x, y=$y       ";

        if (defined $n)
        {
                $times=$n;
        }


        while ($times > 0)
        {
                if ($p->ping($host, 1))
                {
                        print "$host is reachable.\n";
                        $cons->WriteAttr($good,$x,$y,0,0);
                        push(@success,$host);
                }
                else
                {
                        print "$host is NOT reachable.\n";
                        $cons->WriteAttr($bad,$x,$y,0,0);
                        push(@fail,$host);
                }

                $times--;
                $y++;
        
        }

        
}





[EMAIL PROTECTED];
[EMAIL PROTECTED];
[EMAIL PROTECTED];

if ($count!= 1) { $plural_host="s"; }
if ($successcnt == 1)
{
        $plural_success="was";
}
else
{
        $plural_success="were";
}

if ($failcnt == 1)
{
        $plural_fail="was";
}
else
{
        $plural_fail="were";
}

print "\nOut of $count host$plural_host, $successcnt $plural_success
reachable, $failcnt $plural_fail NOT\n";






open(LAST,"> ping-pl.last") || warn "Couldn't update history file\n";
foreach (@hosts)
{
        print LAST "$_\n";
}
close(LAST);







sub noCfg
{
        $host="x";
        until ($host eq '')
        {
                print "Add host to ping list: ";
                chomp($host=<STDIN>);
                if ($host ne '')
                {
                        push(@hosts,$host);
                }
        }

        return 0;
}





sub range
{
        if ($_[0] gt '')
        {
                $first=shift(@_);
                if  ($_[0] gt '')
                {
                        $last = shift(@_);
                }
                else
                {
                        print "Enter the LAST IP in the range: ";
                        chomp($last=<STDIN>);
                }

        }
        else
        {
                print "Enter the first IP in the range: ";
                chomp($first=<STDIN>);
                print "Enter the last IP in the range: ";
                chomp($last=<STDIN>);
        }

        until ($first eq $last)
        {
                #print "DEBUG: first=$first, last=$last\n";
                push(@hosts,$first);
                ($oct1,$oct2,$oct3,$oct4) = split(/\./,$first);
                $oct4++;
                if ($oct4 == 256)
                {
                        $oct4=0;
                        $oct3++;
                        if ($oct3 == 256)
                        {
                                $oct3=0;
                                $oct2++;
                                if ($oct2 == 256)
                                {
                                        $oct2=0;
                                        $oct1++;
                                        if ($oct1 == 256)
                                        {
                                                die "Bad IP Range!\n";
                                        }
                                }
                        }
                }

                $first = join('.',$oct1,$oct2,$oct3,$oct4);
        }

        push(@hosts,$last);
}

### END ping.pl### END ping.pl
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to