开发者

Why is the netmask in Net::IP set wrong?

I'm trying to get a little script to recognize the SNMP data from a query to store it in a database. But I'm stuck when processing the data with the Net::IP CPAN module. If I define the network string there is no problem, but I have IP and mask in separate strings, and no matter how I join them, the module always set the netmask to /32.

I tried it like this:

my $net = "${$query_snmp->{$tablekey}}{'ipRouteMask'}/${$query_snmp->{$tablekey}}{'ipRouteDest'}";
my $IP = new Net::IP ($net) or die (Net::IP::Error());

But all the IP objects are always created with /32 no matter what I set the netmask to. If i define a string like 192.168.0.0/开发者_运维技巧20 or anything on the same string I find no problem.

What am I missing?

Network -> 192.168.65.64/255.255.255.248

IP  : 192.168.65.64
LASTIP  : 192.168.65.64
Sho : 192.168.65.64
Bin : 11000000101010000100000101000000
Int : 3232252224
*** Mask: 255.255.255.255 *** wtf ??
Last: 192.168.65.64
Len : 32
Size: 1
Type: PRIVATE
Rev:  64.65.168.192.in-addr.arpa.


From the manual: A Net::IP object can be created from a single IP address, or from a Classless Prefix, ...

I think you've chosen to specify a classless network, provided that you insert a valid network specifier, it works for me.

#!/usr/bin/perl -w    
use Net::IP;
my $ip = new Net::IP('112.198.64.0/18') or die (Net::IP::Error());
print ("IP  : ".$ip->ip()."\n");
print ("Sho : ".$ip->short()."\n");
print ("Bin : ".$ip->binip()."\n");
print ("Int : ".$ip->intip()."\n");
print ("Mask: ".$ip->mask()."\n");
print ("Last: ".$ip->last_ip()."\n");
print ("Len : ".$ip->prefixlen()."\n");
print ("Size: ".$ip->size()."\n");
print ("Type: ".$ip->iptype()."\n");
print ("Rev:  ".$ip->reverse_ip()."\n");

will output:

IP  : 112.198.64.0
Sho : 112.198.64
Bin : 01110000110001100100000000000000
Int : 1892040704
Mask: 255.255.192.0
Last: 112.198.127.255
Len : 18
Size: 16384
Type: PUBLIC
Rev:  64.198.112.in-addr.arpa.

However, if you've entered "192.168.65.64/255.255.255.248", this is not a format accepted by Net::IP, you've to use instead "192.68.65.64/29", see this table. In this case it will work correctly:

IP  : 192.168.65.64
Sho : 192.168.65.64
Bin : 11000000101010000100000101000000
Int : 3232252224
Mask: 255.255.255.248
Last: 192.168.65.71
Len : 29
Size: 8
Type: PRIVATE
Rev:  64.65.168.192.in-addr.arpa.

When you use the format with the full netmask, since it's not recognized, it gets just the IP and will give you the netmask of 255.255.255.255


thank you ! I finally used a simillar module, that doest requier to transform the ip mask to dec notation.

Net::Netmask -> https://metacpan.org/pod/Net::Netmask

CONSTRUCTING

Net::Netmask objects are created with an IP address and optionally a mask. There are     many forms that are recognized:

'216.240.32.0/24' The preferred form.

'216.240.32.0:255.255.255.0'
'216.240.32.0-255.255.255.0'
'216.240.32.0', '255.255.255.0'
'216.240.32.0', '0xffffff00'
'216.240.32.0 - 216.240.32.255'
'216.240.32.4'
A /32 block.

'216.240.32'
Always a /24 block.

'216.240'
Always a /16 block.

'140'
Always a /8 block.

'216.240.32/24'
'216.240/16'
'default' or 'any'
0.0.0.0/0 (the default route)
'216.240.32.0#0.0.31.255'

Thank you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜