#!/usr/bin/perl -w ################################################################################# #This program consists of two parts. # #The first part is a xml parser that reads out the IPs and names of # #a file given at the start prompt. The program must therefore be called # #with it's name and the filename of the xml-file as parameter: # #e.g. "perl walker.pl topo.xml" (topo is then the xml-file that must be parsed # #The second part of the program is based on the standard snmpwalk by Dave Town. # #It reads different OIDs and extracts their infos saving them in different # #arrays. It then creates a textfile with ALL entries. This text file # #"alllist.txt"(v 1.0) is then parsed again and all unwanted entries are deleted.# #the final result is saved in a textfile called "finallist.txt"(v 1.0). # #The result is set up as default (v 1.0) like: # #ip;name;serial number;part number;description # #The "alllist.txt", "ipfile.txt" and the "namefile.txt" are afterwards deleted # #Program. Walker.pl Version:1.0 By: Ronin # ################################################################################# use Net::SNMP qw(oid_lex_sort oid_base_match SNMP_VERSION_1 DEBUG_ALL); use strict; use Getopt::Std; use warnings; use diagnostics; use XML::Simple; use Data::Dumper; use vars qw($ip $name $response $response2 $response3 @final1 @final2 @final3); ################################################################################# #This is the first part of the program that parses the information from the # #xml-file saving the resulting IPS in "ipfile.txt"(v1.0) and the resulting # #device names in "namefile.txt"(v1.0) # #Program. Walker.pl Version:1.0 By: Ronin # ################################################################################# #open xml file taking the parameter from the prompt my $xmlfile = $ARGV[0]; my $ref = eval { XMLin($xmlfile) }; #erase or create the ipfile and the namefile open ERASER, ">ipfile.txt"; close ERASER; open ERASER, ">namefile.txt"; close ERASER; open ERASER, ">finallist.txt"; close ERASER; open ERASER, ">alllist.txt"; close ERASER; #see if open worked if ($@) { print "XML Read ERROR"; } else { #go to IPAddress tag and read infos into file foreach my $itemip (@{$ref->{Layer2Details}->{Device}}) { my @ipliste = $itemip->{IPAddress}; my @sorted = @ipliste; open OUTIP, ">>ipfile.txt"; print OUTIP @sorted, "\n"; close OUTIP; } #go to DeviceName tag and read infos into file foreach my $itemname (@{$ref->{Layer2Details}->{Device}}) { my @nameliste = $itemname->{DeviceName}; my @sortedname = @nameliste; open OUTNAME, ">>namefile.txt"; print OUTNAME @sortedname, "\n"; close OUTNAME; } } ################################################################################# #The second part of the program start here and extracts the information wanted # #creating the result file # #Program. Walker.pl Version:1.0 By: Ronin # ################################################################################# #open ipfile open IPFILE, "ipfile3.txt" or die "Can't get IPs - $!\n"; open NAMES, "namefile3.txt" or die "Can't get IPs - $!\n"; #erase or create the finallist open ERASER, ">finallist.txt"; close ERASER; #Sets knots und community my $community = 'public'; my $ifSer = '.1.3.6.1.2.1.47.1.1.1.1.11'; my $ifPart = '.1.3.6.1.2.1.47.1.1.1.1.13'; my $ifDesc = '.1.3.6.1.2.1.47.1.1.1.1.2'; #loop the program until all IPs have been worked with while ( $ip = , $name = ) { #save ip and name chomp $ip; chomp $name; #delete the .bc.de.bic extention $name =~ s/\.bc\.de\.bic$//; #print which IP is being worked with print "Got: $ip\n"; #open session my ( $s, $e ) = Net::SNMP->session( -hostname => $ip, -community => $community, -port => 161 ); if (!defined($s)) { print ("Falsch nix is!"); } #bind the OIDs to arrays using varbindlist #to keep the original order of the entries my @args = ( -varbindlist => [$ifSer] ); my @args2 = ( -varbindlist => [$ifPart] ); my @args3 = ( -varbindlist => [$ifDesc] ); #check SNMP version if ($s->version == SNMP_VERSION_1) { #extract the wanted information adn save them into an array #using the push statement so the next entry is added at the #end of the array while (defined($s->get_next_request(@args))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifSer, $_)) { last; } $response=($s->var_bind_list->{$_}); push @final1, $response; @args = (-varbindlist => [$_]); } while (defined($s->get_next_request(@args2))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifPart, $_)) { last; } $response2 = ($s->var_bind_list->{$_}); push @final2, $response2; @args2 = (-varbindlist => [$_]); } while (defined($s->get_next_request(@args3))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($ifDesc, $_)) { last; } $response3=($s->var_bind_list->{$_}); push @final3, $response3; @args3 = (-varbindlist => [$_]); } } #if version is not 1 then use this else { push(@args, -maxrepetitions => 25); outer: while (defined($s->get_bulk_request(@args))) { my @oids = oid_lex_sort(keys(%{$s->var_bind_list})); foreach (@oids) { if (!oid_base_match($ifSer, $_)) { last outer; } #print("%s => %s\n", $_, $s->var_bind_list->{$_}); # Make sure we have not hit the end of the MIB if ($s->var_bind_list->{$_} eq 'endOfMibView') { last outer; } } # Get the last OBJECT IDENTIFIER in the returned list @args = (-maxrepetitions => 25, -varbindlist => [pop(@oids)]); } } #create the file with ALL entries open OUTPUT, ">alllist.txt"; #print in column style for my $i (0 .. $#final1) { print OUTPUT "$ip;$name;$final1[$i];$final2[$i];$final3[$i]\n" } close OUTPUT; #empty the arrays @final1=(); @final2=(); @final3=(); #create the file with the wanted entries open FINISH, "; open WRITER,">>finallist.txt"; print WRITER @data; close WRITER; close FINISH; #close the SNMP session $s->close(); } #delete the alllist.txt, ipfile.txt and namefile.txt file unlink 'alllist.txt','ipfile.txt','namefile.txt'; exit 0;