Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

New to Perl

by NastyPenguin (Initiate)
on Nov 17, 2010 at 23:02 UTC ( #872080=perlquestion: print w/replies, xml ) Need Help??
NastyPenguin has asked for the wisdom of the Perl Monks concerning the following question:

So kinda been thrown to the fishes and am in urgent need of some coding wisdom. I need to be able to create a log with a list of IP address' in it between a set range that is input by the user. I'm using Net::Netmask, Data:Validate:Range:Parse, Net:IPv4 as the major modules. Am I missing a module? have gone through so many forums looking for the answer but get brief explanations I kinda like to know whats happening instead of just copy pasting. Thanks in advance.

Replies are listed 'Best First'.
Re: New to Perl
by oko1 (Deacon) on Nov 18, 2010 at 02:27 UTC

    Heh. I used to live in an Italian neighborhood in Brooklyn, and "thrown to the fishes" meant that you'd never have any kind of problem again (Perl included.)

    So... what are you trying to do? "need to {...} create a log with a list of IP address{es}" is kinda obscure - and adding a list of modules to it doesn't help. Can you explain exactly what you're trying to do? If you just need to generate a list of IPs between two endpoints, it's not that hard - and doesn't require any modules. So, details, please?

    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      Okay so wrong term to use maybe wolves instead of fishes. So here is what I'm wanting to do: Enter the first and last address of a network. Have to code for this already and have it validating as IPv4. Then create a log file with the entire range between these user inputs so that I have a full list of all IP's on the network. Following that I'd like to be able to ping all IP's in that logged range and determine if a device is located on that address. Then re-log only the discovered devices and grab the information about the discovered device. Hope that makes a bit more sense.
Re: New to Perl
by NetWallah (Abbot) on Nov 18, 2010 at 04:52 UTC
    I have used cpan://NetAddr::IP for this purpose before. See the method
    ->hostenumref() Faster version of ->hostenum(), returning a reference to a list.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: New to Perl
by SimonClinch (Deacon) on Nov 18, 2010 at 10:24 UTC
    It's a matter of opinion, but I believe insisting on solving with modules will hinder your ability to code. This one seems simple enough. Just convert to binary before comparing input with binary-converted limits of the range :-
    my $limit1 = ''; my $limit2 = ''; my ($bl1,$bl2) = (cnvb($limit1),cnvb($limit2)); while (<>) { chomp; my $bin = cnvb($_); if ( $bin >= $bl1 and $bin <= $bl2 ) { print "$_\n"; } } sub cnvb { my @mask = split /\./, shift(); $#mask == 3 or die "bad IP address"; my $ret = 0; for my $i ( 0 .. 3 ) { $ret *= 256; $ret += $mask[$i]; } return $ret; }

    One world, one people

Re: New to Perl
by Utilitarian (Vicar) on Nov 18, 2010 at 13:38 UTC
    Not sure if I understand your request correctly, however nmap returns a list of network services for each host on a given segment.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      SO i've kinda got though the first part now I need to ping from the first entered address through to the last entered address. here is the code i have so far
      #A script to identify any device that is using factory settings within + the network ie default username and password. #The script is designed so that the user (Network Admin) enters in bot +h the start and end of the IP range of the network and a address list + #is then generated and logged.After logging the range of the network e +ach address will be pinged to detect if a device is located on that a +ddress #A second log file is created and the active address are then listed. +On a second round the script will grab any information the device is #showing and log this as well. The known device locatations are then t +o be cross referenced with a default list of device usernames and pas +swords. #If a device is able to be access with one of the defaults it will be +listed as TRUE in the log and the device will be identified as well. #Once complete an email will be sent to the admin containing the three + log files. #!/usr/bin/perl -w use strict; use warnings; use diagnostics; #Enter and Validate IP Address Range. use Data::Validate::IP qw(is_ipv4 is_ipv6); print "Enter IP Range Starting Address:"; my $answera = <STDIN>; if(is_ipv4($answera)){ print "Looks like an ipv4 address\n"; } else { print "Not an ipv4 address\n"; print "\nReEnter Address:"; my $answera = <STDIN>; } print "\nEnter IP Range Finishing Address:"; my $answerb = <STDIN>; if(is_ipv4($answerb)){ print "Looks like an ipv4 address"; } else { print "Not an ipv4 address\n"; print "\nReEnter Address:\n"; my $answerb = <STDIN>; } use Net::IP; use NetAddr::IP; my $ipa = new NetAddr::IP->new($answera); my $ipb = new NetAddr::IP->new($answerb); print "\nNetwork starts at $ipa and finshes at $ipb\n"; #Create a log file with the active IP address and corresponding device + names. open (MYLOG, '>>addressrange.txt'); print MYLOG "Network starts at $ipa and finshes at $ipb"; print MYLOG "$ipa +(1)"; close (MYLOG); #Create range between the two entered IP address' #Ping address' within the range. Log these in the log file. use Net::Ping; my $p = Net::Ping->new(); $p->ping($ipa [,5]); print "$ipa ia alive.\n" if $p->ping($ipa); $p->close();
      I've read the syntax and diagnostics but it's not making any sense sorry.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-08-22 16:07 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (337 votes). Check out past polls.