Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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(''); while ($ip < $ip->broadcast) { print "$ip\n"; $ip ++; } exit;

This will generate a list that looks like this:

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

Note that I have removed the /29 and the 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?


Replies are listed 'Best First'.
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(''); 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(''); 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 aaron_baugher (Curate) 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(''); $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(''); 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(''); 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!
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 '' 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948789]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2017-11-21 01:45 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (294 votes). Check out past polls.