in reply to Reading IP from a file and SNMP

Probably the simplest module to work with for getting at arbitrary MIBs is Net::SNMP - the following example (hacked from the code in my Net::SNMP::Interfaces ) walks the ifTable for hosts who's IP are specified in the file ipfile.txt (one per line) and and prints the description of the interface:

use Net::SNMP; use strict; use warnings; + open IPFILE, "ipfile.txt" or die "Can't get IPs - $!\n"; + my $community = 'public'; my $ifIndex = '1.3.6.1.2.1.2.2.1.1'; my $ifDescr = '1.3.6.1.2.1.2.2.1.2'; + while ( my $ip = <IPFILE> ) { chomp $ip; + my ( $session, $error ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); my $response; + if ( defined( $response = $session->get_table($ifIndex) ) ) { foreach my $index ( values %{$response} ) { my $this_desc = "$ifDescr.$index"; my $description; if ( defined( $description = $session->get_request($this_d +esc) ) ) { print values %{$description}, "\n"; } } + } + $session->close(); }

Hope that helps

/J\

Replies are listed 'Best First'.
Re^2: Reading IP from a file and SNMP
by theroninwins (Friar) on Aug 27, 2004 at 07:33 UTC
    WOW thanks fpr all your comments. They really helped. I got the thing more or less because it has to do a lot more (redirect the output and filter it then rerun the prog with the new knot and so on because i need the Name of the Devive e.g Power Device and the coresponding serial no.) but i guess i figure that out myself. but here is the bigger problem: I can get all the results i want correctly as long as they are numeric, but if the answer is e.g SNI05638854 or 1300 watt supply AC i get this:

    Argument "SNI05638854" isn't numeric in numeric le (<=) at C:/Perl/lib/Net/SNMP/Message.pm line 998, <IPFILE> line 1 (#1) (W numeric) The indicated string was fed as an argument to an operator that expected a numeric value instead. If you're fortunate the message will identify which operator was so unfortunate.

    the IP file has only an IP in it. I based my prog on the one by gellyfish. (Tanks for that one). Any idea why this message comes?? I really must say i start to like perl but as i said before i am very new to it :-)

      I think your problem is either that you are misinterpreting your MIB and trying to use one of your returned values as part of an OID or your MIB is really strange - it would probably be useful for you to look at the output of 'snmpwalk' for the top level OID you are looking at (I'm not sure what the tools for this on windows are like however) - there is not much we can do from here because you are using a proprietary MIB.

      /J\

        Ok i got it now I noticed that the MIB does not have an Indexfile, so I took another Entry that does more or less the same and got what I needed. Still one more little thing and i hope i will not annoy you all with my beginners questions..... the stuff that comes out on screen: I know how to redirect that to a file but how do i format it? I need 3 colums all seperated with ';'. I figured out that maybe something like print '1',';','2',';','3',';' might work ?