in reply to Reading IP from a file and SNMP

You're going to want to use the various SNMP modules from CPAN. I'm not extremely familiar with them, but that's where we go to find modules to do what we don't want to have to figure out cause someone else already did. :-)

Oh, and welcome aboard!

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

Replies are listed 'Best First'.
Re^2: Reading IP from a file and SNMP
by habit_forming (Monk) on Aug 26, 2004 at 14:17 UTC
    Well I like to run SNMP over tcp so i do this:
    sub get_my_oid { my ($host, $oid, undef) = @_; my $data = ''; my @errors = (); my $options = " -l authPriv -u $User -X $Pass". " -A $Pass -v 3 ". " -t $Timeout -r $Retries -Ouvq "; my $result = `snmpwalk $options TCP:${host} $oid`; if(defined $result && (($? >> 8) == 0)) { my @lines = split "\n", $result; foreach my $oid_line (@lines) { $oid_line = substr $oid_line, 1; #front quote chop $oid_line; #ending quote #unescape all of my quotes. \" => " $oid_line =~ s/\\\"/\"/og; #it is a part of our data. $data .= $oid_line; } } else { push @errors, "Error with the snmp walk command: $!\n"; return (undef, \@errors); } return ($data, undef); }

    Note that I actually use this code and it does work. BUT! Use at your own risk.

    --habit
Re^2: Reading IP from a file and SNMP
by hsinclai (Deacon) on Aug 26, 2004 at 14:11 UTC
    Try starting off with module Net::SNMP, to get single values from a remote IP, probably the method $session->get_request is your best bet.. it's pretty well documented in perldoc Net::SNMP.. However you probably have to put your community in the file.. if this is undesirable, you could use module Term::ReadKey to allow "safe" reading of the password from the command line..

    As far as getting the IP addresses out of a file, if they are listed one IP Address per line with nothing else included in the file itself: just read the file, put the contents in an array, then loop your SNMP get operations for each item in the array roughly something like:
    open IPFILE,'<',"/tmp/ipfile.txt"; my @ip_list = <IPFILE>; close IPFILE; foreach my $ip_in_list ( @ip_list ) { ... ($session, $error) = Net::SNMP->session( -hostname => $ip_in_list, ... => ..., .. all other options ...; ); $newval = $session->get_request( [-callback => sub {},] [-delay => $seconds,] [-contextengineid => $engine_id,] [-contextname => $name,] -varbindlist => \@oids, ); # now do something with the data found in $newval # use -callback to point to a subroutine where you put further operati +ons to be carried out on $newval }
    The above is more or less straight from the doc, and your situation is probably more complicated than that, but maybe this helps get started?

Re^2: Reading IP from a file and SNMP
by theroninwins (Friar) on Aug 26, 2004 at 13:50 UTC
    There is still the question of the reading of the IPs in the file?? Thanks for wellcoming me and for the help already... I got some interseting piece of code :-) just got to understand it now.
      I highly suggest getting copies of Learning Perl, Programming Perl, and The Perl Cookbook.

      To get you started, you can read files like this:

      open FH, '<', $filename or die ("Can't open $filename"); #FH is a "filehandle", and the '<' means "open in read mode" #('>' would be write) while (<FH>) { #the angles around FH mean "read a line" work_with_the_line($_); # $_ holds the current line in this case # work_with_the_line would be a subroutine defined by you } close FH; #don't forget to close files!
      Much perl code is out there "in the wild", I highly suggest searching to see if someone has solved a similar problem, then reading and learning from their source.
      --
      $me = rand($hacker{perl});
      Well it depends on how they are stored in the file. If there is only one IP per line, this code will do:
      my @ip_array = (); if(open(F_IP,"<$filename")) { my %ip_hash = (); foreach my $ip (<F_IP>) { chomp($ip); #No duplicates! $ip_hash{$ip} = 1; } close(F_IP); @ip_array = keys(%ip_hash); } else { die "AHHHHH! Error Error go away!!"; } #Just keep doing your thing man...


      Note that I wrote this on the fly and have not tested it. It should give you a good start though. =)

      --habit