Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

ip address list

by mlebel (Hermit)
on Jan 19, 2012 at 14:36 UTC ( #948789=perlquestion: print w/ replies, xml ) Need Help??
mlebel has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, Quick question for you. I wasn't able to find much on this online.
My goal is to generate a list of usable ip addresses within *any* subnets. so far I have the following:

#!/usr/bin/perl use NetAddr::IP; use strict; my $ip = new NetAddr::IP('10.2.9.0/29'); while ($ip < $ip->broadcast) { print "$ip\n"; $ip ++; } exit;

This will generate a list that looks like this:

10.2.9.0/29
10.2.9.1/29
10.2.9.2/29
10.2.9.3/29
10.2.9.4/29
10.2.9.5/29
10.2.9.6/29

but this is not quite what I need.
what i need is a list that would look like this:

10.2.9.2
10.2.9.3
10.2.9.4
10.2.9.5
10.2.9.6

Note that I have removed the /29 and the 10.2.9.1 as this is the gateway address for that subnet.
(all gateway addresses would always be the first available address within a given subnet.)
And this subnet would come from the command line, therefore it would be changing.

How could I achieve this goal?

Thanks,
Marc

Comment on ip address list
Download Code
Re: ip address list
by wwe (Friar) on Jan 19, 2012 at 16:03 UTC
    Hi mlebel,

    this produces a list you are looking for

    use strict; use warnings; use Net::Netmask; my $block = Net::Netmask->new('192.168.1.128/25'); print "1st element: ", $block->nth(1), "\n"; print "last element: ", $block->nth(-2), "\n"; my $i = 1; while (1) { print "ip adress #$i:", $block->nth($i), "\n"; last if $block->nth($i) eq $block->nth(-2); $i++; }
    Maybe there is a better method in the Net::Netmask

      Thanks wwe! It does just about what I need. I have modified it slightly to get the results that I was looking for. Here is the code:

      #!/usr/bin/perl use strict; use warnings; use Net::Netmask; my $block = Net::Netmask->new('10.2.9.0/29'); my $i = 1; while (1) { print $block->nth($i), "\n"; last if $block->nth($i) eq $block->nth(-2); $i++; }

      All I need to do now is to find a way to remove the first ip(the gateway ip)... any suggestions? thanks

      Hi mlebel

      as you may know this is not a code writing service. I'm glad to help you but you should also make your own efforts.

      The code I provided creates a network object and loops over all adresses inside of network. I think it is very easy to find out how to exclude the first address.

        thanks wwe..

        If I knew how to write it, i wouldn't be asking for help on this forum.

        If you don't want to help, then please keep your help and comments for yourself in the future. We are all here to learn and help eachother out.

        I for one have given full answers to others many times online in the past..

        This code is part of a much bigger script and Oh, I wrote all of the rest.. it's normal not to know the answer to something at some point. Lucky for you, I didn't ask how to incorporate the results into an array or anything else like that, I figured all that out by my self.. would you like to have the answer?(this way I can share back with you)

        Besides, this will most likely help someone else in the future also. Are you against that too?

        Thanks for your help anyway

Re: ip address list
by JavaFan (Canon) on Jan 19, 2012 at 16:10 UTC
    So, you're asking how to remove '/29' from a string, and how to remove the first (second?) element of a list?

    You also removed '10.2.9.0' from the list, but you didn't say why.

      Hi, sorry about that, your right.
      I need to remove the network address as well as the gateway address.
      I will be using this code within another script that checks internet usage, so I simply need the hosts addresses and not the network, gateway or broadcast addresses.
Re: ip address list
by aaron_baugher (Deacon) on Jan 19, 2012 at 17:12 UTC

    So you want to skip the first two addresses, then print up to the broadcast address, removing the netmask. The simplest way would be to increment the address twice before your loop. Then I'd probably use split to remove the netmask portion, though you could certainly use a regex. I tend to use split when I'm thinking "get everything before a certain character". So:

    my $ip = NetAddr::IP->new('10.2.9.0/29'); $ip++; # skip network address $ip++; # skip presumed gateway address while( $ip < $ip->broadcast ){ my $iponly = (split '/', $ip++)[0]; #get part before first /, then inc +rement the ip print "$iponly\n"; }

    Looks like you could also use NetAddr::IP's functionality to get the address by itself, as in $ip->addr.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      That was it Aaron B. Thank you very much, it worked as expected. Thank you to all of you who helped me resolve this problem! Marc
Re: ip address list
by ikegami (Pope) on Jan 19, 2012 at 19:55 UTC
Re: ip address list
by ikegami (Pope) on Jan 19, 2012 at 19:59 UTC

    By the way, you might find the hostenum method useful.

    my $net = NetAddr::IP->new('10.2.9.0/29'); my $seen_gateway; for my $ip ($net->hostenum) { next if !$gateway++; print $ip->addr, "\n"; }

    Update: Oops, you wanted to skip the first address.

    my $net = NetAddr::IP->new('10.2.9.0/29'); my @hosts = $net->hostenum; for my $ip (@hosts[1..$#hosts]) { print $ip->addr, "\n"; }
      Hi ikegami, that's definitely another valid way of doing it, thank you for your input!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948789]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2014-07-24 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls