perl地址转换

#!/usr/bin/perl -w #### 将十进制数转换成8为二进制 sub dectobin {     substr(unpack("B31",pack("N",shift)),-8); } #### 将32位二进制转换成十进制 sub bintodec {     unpack("N",pack("B32",substr("0" x 32 . shift,-32))); } #### 将二进制表示的 IP/子网掩码转换成十进制形式 sub ipmask_bin2dec {     my $prefix = "";     my $result;     map { $result .= $prefix . &bintodec($_); $prefix = ".";     } split (/\./,shift);     return $result; } #### 将十进制表示的 IP/子网掩码转换成二进制形式 sub ipmask_dec2bin {     my $prefix = "";     my $result;     map { $result .= $prefix . &dectobin($_); $prefix = ".";     } split (/\./,shift);     return $result; } #### 将 CIDR 表示的子网掩码转换成十进制形式 sub ipmask_cidr2dec {     my $cidr_num = shift();     return if ($cidr_num < 0 || $cidr_num > 32);     (my $mask = 1 x $cidr_num . 0 x (32-$cidr_num)) =~         s/(\d{8})(\d{8})(\d{8})(\d{8})/$1.$2.$3.$4/; print "#####$mask####\n";     return &ipmask_bin2dec($mask); } my $dec_ip = "192.168.2.1"; my $dec_mask = "255.255.255.128"; my $cidr_num = 25; #my $bin_ip = &ipmask_dec2bin($dec_ip); #my $bin_mask = &ipmask_dec2bin($dec_mask); my $bin_mask2 = &ipmask_cidr2dec($cidr_num); my $test = &dectobin(32); print "Decimal IP: $dec_ip\n"; print "Binary IP: $bin_ip\n"; print "Decimal Netmask: $dec_mask\n"; print "Binary Netmask: $bin_mask\n"; print "CIDR Netmask: $cidr_num = $bin_mask2\n";

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...