Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: SNMP - cont

by theroninwins (Friar)
on Aug 30, 2004 at 12:44 UTC ( #386901=note: print w/replies, xml ) Need Help??


in reply to Re^2: SNMP - cont
in thread SNMP - cont

Ok I got it going somehow, now it uses temp files but it works :-) Here it is my first prog :-).

#!/usr/bin/perl -w use Net::SNMP; #use strict; use warnings; use diagnostics; open IPFILE, "ipfile.txt" or die "Can't get IPs - $!\n"; my $community = 'public'; my $ifIndex = '1.3.6.1.2.1.47.1.1.1.1.6'; #1.3.6.1.2.1.2.2.1.1 + 1.3.6.1.2.1.47.1.1.1.1.12 my $ifDescr = '1.3.6.1.2.1.47.1.1.1.1.11'; #1.3.6.1.2.1.47.1.1.1.1. +13 my $ifDescr2 = '1.3.6.1.2.1.47.1.1.1.1.2'; #1.3.6.1.2.1.47.1.1.1.1. +13 while ( my $ip = <IPFILE> ) { chomp $ip; print "Got: $ip\n"; + 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} ) #values { my $this_desc = "$ifDescr.$index"; my $description; if ( defined( $description = $session->get_request($this_d +esc) ) ) { my @serial = values %{$description}; open OUTPUT1, ">>serlist.txt"; print OUTPUT1 @serial, "\n"; close OUTPUT1; } } foreach my $index ( values %{$response} ) #values { my $this_desc = "$ifDescr2.$index"; my $description2; if ( defined( $description2 = $session->get_request($this_ +desc) ) ) { my @desc = values %{$description2}; open OUTPUT2, ">>desclist.txt"; print OUTPUT2 @desc, "\n"; close OUTPUT2; } } open(OUT, "serlist.txt"); open(OUT2, "desclist.txt"); my @outlist; while (<OUT>) { my %hash; my @temp = split(/;/,$_); $hash{'file1'} = $temp[0]; $hash{'file2'} = <OUT2>; #Return entfernen foreach (values %hash) { $_ =~ s/\n//g; } push(@outlist,\%hash); } close(OUT); open FINOUT, ">>finallist.txt"; print FINOUT "$ip\n"; foreach my $hashref (@outlist) { print FINOUT "$hashref->{'file1'};$hashref->{'file2'}\n"; } print FINOUT "\n"; close FINOUT; } $session->close(); }


... maybe you have an idea to do it without those temps.

Replies are listed 'Best First'.
Re^2: SNMP - cont
by theroninwins (Friar) on Sep 02, 2004 at 05:49 UTC
    OK thanks for all of your help everyone and here it is the work I made all the tmie:
    #!/usr/bin/perl -w ############################################################# #This program generates a list of informations by parsing an# #xml file and then extrating the informations via SNMP, # #saving the result in a textfile. # #Ver:1.0 written by:ronin # ############################################################# use Net::SNMP; use strict; use warnings; use diagnostics; use XML::Simple; use Data::Dumper; ###################################################################### +############ #This part of the program generates an IP file with one ip address per + line. # #It reads the IPAddress tagged information from the xml file it parses +. # #The IP addresses are then saved in a txt file. + # #The information saved here are later used and referred to as ipfile.t +xt # #Ver:1.0 written by :ronin # ###################################################################### +############ #open xml file my $xmlfile = $ARGV[0]; my $ref = eval { XMLin($xmlfile) }; #erase or creat the ipfile open ERASER, ">ipfile.txt"; close ERASE; #see if open worked if ($@) { print "XML Read ERROR"; } else { #go to IPAddress tag and read infos into file foreach my $item (@{$ref->{Layer2Details}->{Device}}){ my @ipliste = $item->{IPAddress}; my @sorted = @ipliste; open OUTIP, ">>ipfile.txt"; print OUTIP @sorted, "\n"; close OUTIP; } } #################################################################### #The second part of the porgram uses the above saved IP addresses # #reading them from the file and looping for as many times as # #necessary. It gets all informations from manually added MIB knots # #and saves these informations in seperate files. Then it parses all# #files and generates a full unedited listing saving it in another # #file. This unedited file is then reopened and edited so that only # #the wanted usefull informations remains. The final file is then # #saved (in Ver 1.0 as finallist.txt). # #Ver:1.0 written by: ronin # #################################################################### #open ipfile open IPFILE, "ipfile.txt" or die "Can't get IPs - $!\n"; #Sets knots und community my $community = 'public'; my $ifIndex = '1.3.6.1.2.1.47.1.1.1.1.6'; my $ifDescr = '1.3.6.1.2.1.47.1.1.1.1.11'; my $ifDescr2 = '1.3.6.1.2.1.47.1.1.1.1.2'; #erase or create files open ERASER, ">serlist.txt"; close ERASE; open ERASER, ">desclist.txt"; close ERASE; open ERASER, ">alllist.txt"; close ERASE; open ERASER, ">finallist.txt"; close ERASE; #looping as long as there are IP addersses while ( my $ip = <IPFILE> ) { chomp $ip; print "Got: $ip\n"; + #open session my ( $session, $error ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); my $response; #goto index table and hash index if ( defined( $response = $session->get_table($ifIndex) ) ) { #get serialnumbers foreach my $index ( values %{$response} ) { my $this_desc = "$ifDescr.$index"; my $description; if ( defined( $description = $session->get_request($this_d +esc) ) ) { #print serialno. to file my @serial = values %{$description}; open OUTPUT1, ">>serlist.txt"; print OUTPUT1 @serial, "\n"; close OUTPUT1; } } #get description foreach my $index ( values %{$response} ) { my $this_desc = "$ifDescr2.$index"; my $description2; if ( defined( $description2 = $session->get_request($this_ +desc) ) ) { #print describtion to file my @desc = values %{$description2}; open OUTPUT2, ">>desclist.txt"; print OUTPUT2 @desc, "\n"; close OUTPUT2; } } #create final file open(OUT, "serlist.txt"); open(OUT2, "desclist.txt"); my @outlist; while (<OUT>) { my %hash; my @temp = split(/;/,$_); $hash{'file1'} = $temp[0]; $hash{'file2'} = <OUT2>; #delete Return foreach (values %hash) { $_ =~ s/\n//g; } push(@outlist,\%hash); } close(OUT); #print to file open ALLOUT, ">>alllist.txt"; print ALLOUT "$ip\n"; foreach my $hashref (@outlist) { print ALLOUT "$hashref->{'file1'};$hashref->{'file2'}\n"; } close ALLOUT; #scanning the file for useless lines and deleting them and then #saving the rest in a new file open SCAN, "<alllist.txt"; my @data = grep { /^\S+;/ } <SCAN>; open FINOUT, ">>finallist.txt"; print FINOUT @data, "\n"; close SCAN; } #close session $session->close(); }

    It reads all IPAddress-tagged infos from an xml file andf then uses those to get infos from MIBs via SNMP, saves thjem in different files and then creats another file with the result in a certain order like a csv file so it can be automatically read be progrags like CiscoWorks 2000. Thanks again everybody for the help provided.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://386901]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-05-29 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?