Deepika1312 has asked for the wisdom of the Perl Monks concerning the following question:

hello all, i'm trying to find a way in perl, to find subnet address details from host ip address. can you please help. Thanks in advance!!

Replies are listed 'Best First'.
Re: Subnet Address details from ip address
by dsheroh (Monsignor) on Aug 01, 2017 at 10:07 UTC
    Note that it is not possible to derive this information solely from the IP address. While this was possible in the early days of the internet, when you could look at an address and see whether it was in a space reserved for Class A (netmask 255.0.0.0), Class B (255.255.0.0), or Class C (255.255.255.0) networks, the modern IPv4 address space is allocated as CIDR (Classless Inter-Domain Routing) blocks, which can have netmasks with any number of bits set and don't necessarily respect the Class A/B/C boundaries.

    Given an IP address and a netmask, you can calculate the range of addresses, network address, broadcast address, etc., but not from the IP address alone.

Re: Subnet Address details from ip address
by Discipulus (Canon) on Aug 01, 2017 at 10:11 UTC
    Hello Deepika1312 and welcome to the manastery and to the wonderful world of Perl!

    from the little I know about networking what you want is not possible: given the lonely IP address you cannot desume what the netmask value is. Infact the net can be subnetted at will resulting in a serie of a little subnets.

    If you are looking to the local IP configured in some host you can retrieve how the network card is configured but it can be also misconfigured: so you cannot be sure if the mask is the rigth one.

    By other hand if you know the net (not just an IP) you can retrieve useful informations using NetAddr::IP for example (warning windows double quotes!):

    perl -MNetAddr::IP -e "$ip=new NetAddr::IP '189.10.136.129/27'; print +'net ',$ip,' mask ',$ip->mask,' first ',$ip->first,' last ',$ip->last +" net 189.10.136.129/27 mask 255.255.255.224 first 189.10.136.129/27 las +t 189.10.136.158/27

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Subnet Address details from ip address
by thanos1983 (Parson) on Aug 01, 2017 at 08:16 UTC

    Hello Deepika1312,

    Welcome to the Monastery. What you have tried and where you have failed? Show us your sample of code, input and expected output.

    From a quick search on the web I have found a few modules that could possibly help you, have you tried those? For example similar question calculate subnet help, this is good starting point.

    Looking forward to your reply, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!