Скрипт perl ip калькулятор

Скрипт perl ip калькулятор, добавил возможность ввода маски вида /23. ./ipcalc.pl 10.2.2.1 /23 Network mask : 255.255.254.0 Network address : 10.2.2.0 Broadcast address: 10.2.3.255
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl
 
# Get ipaddress and netmask from user
 
my $ipaddr=$ARGV[0];
my $nmask=$ARGV[1];
 
my $netmask2 = 0;
my ($netmask2) = $nmask =~ (/\/(\d{1,2})$/);
if($netmask2>0){
        my $acct = int($netmask2/8);
        my $ost = $netmask2 - ($acct*8);
        my $mask = "";
        for($n=1;$n< =4;$n++){
                if($n<=$acct){
                        $mask=$mask ."". '255';
                }elsif($n-1==$acct){
                        $mask=$mask ."". (256-(2**(8-$ost)));
                }else{
                        $mask=$mask ."". "0";
                }
                if($n<=3){
                        $mask=$mask ."". ".";
                }
        }
        if($mask ne ""){
                $nmask = $mask;
        }
}
 
my @addrarr=split(/\./,$ipaddr);
my ( $ipaddress ) = unpack( "N", pack( "C4",@addrarr ) );
my @maskarr=split(/\./,$nmask);
my ( $netmask ) = unpack( "N", pack( "C4",@maskarr ) );
 
# Calculate network address by logical AND operation of addr & netmask
# and convert network address to IP address format
 
my $netadd = ( $ipaddress & $netmask );
my @netarr=unpack( "C4", pack( "N",$netadd ) );
my $netaddress=join(".",@netarr);
 
print "Network mask : $nmask \n";
print "Network address : $netaddress \n";
 
 
# Calculate broadcase address by inverting the netmask
# and do a logical or with network address
my $bcast = ( $ipaddress & $netmask ) + ( ~ $netmask );
my @bcastarr=unpack( "C4", pack( "N",$bcast ) ) ;
my $broadcast=join(".",@bcastarr);
print "Broadcast address: $broadcast\n";
 
# END of program
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Login

Рубрики

Праздники

Армейские праздники

Счетчики

Яндекс.Метрика