<?xml version="1.0" encoding="windows-1252"?>
<node id="392350" title="Getting different things from routers and switches via SNMP" created="2004-09-20 09:29:16" updated="2005-08-15 01:31:46">
<type id="1042">
CUFP</type>
<author id="386009">
theroninwins</author>
<data>
<field name="doctext">
This prog gets different things from routers which ips and names it parses from an xml file and writes the result in a textfile.

&lt;p&gt;&lt;readmore&gt;&lt;code&gt;#!/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, "&gt;ipfile.txt";
close ERASER;
open ERASER, "&gt;namefile.txt";
close ERASER;
open ERASER, "&gt;finallist.txt";
close ERASER;
open ERASER, "&gt;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-&gt;{Layer2Details}-&gt;{Device}})
		{
           
            		my @ipliste = $itemip-&gt;{IPAddress};
                
            		my @sorted = @ipliste;
            		open OUTIP, "&gt;&gt;ipfile.txt";
            		print OUTIP @sorted, "\n";
            		close OUTIP;
        	}

        #go to DeviceName tag and read infos into file
        foreach my $itemname (@{$ref-&gt;{Layer2Details}-&gt;{Device}})
		{
           
           		my @nameliste = $itemname-&gt;{DeviceName};
                
            		my @sortedname = @nameliste;
            		open OUTNAME, "&gt;&gt;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, "&gt;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 = &lt;IPFILE&gt;, $name = &lt;NAMES&gt; )
{
    
    #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-&gt;session(
        -hostname  =&gt; $ip,
        -community =&gt; $community,
        -port      =&gt; 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 =&gt; [$ifSer]
	);
	my @args2 = (
	-varbindlist =&gt; [$ifPart]
	);
	my @args3 = (
	-varbindlist =&gt; [$ifDesc]
	);



	#check SNMP version
	if ($s-&gt;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-&gt;get_next_request(@args))) 
   		{
      			$_ = (keys(%{$s-&gt;var_bind_list}))[0];
 	    		if (!oid_base_match($ifSer, $_)) { last; }
      			$response=($s-&gt;var_bind_list-&gt;{$_});   
      			push @final1, $response;
      			@args = (-varbindlist =&gt; [$_]);
		}
 
		while (defined($s-&gt;get_next_request(@args2))) 
		{
      			$_ = (keys(%{$s-&gt;var_bind_list}))[0];
      			if (!oid_base_match($ifPart, $_)) { last; }
      			$response2 = ($s-&gt;var_bind_list-&gt;{$_}); 
      			push @final2, $response2; 
      			@args2 = (-varbindlist =&gt; [$_]);
   		}

		while (defined($s-&gt;get_next_request(@args3))) 
   		{
      			$_ = (keys(%{$s-&gt;var_bind_list}))[0];
 	    		if (!oid_base_match($ifDesc, $_)) { last; }
      			$response3=($s-&gt;var_bind_list-&gt;{$_});   
      			push @final3, $response3;
      			@args3 = (-varbindlist =&gt; [$_]);
		}
	}
	
	#if version is not 1 then use this
	else 
	{

		 		push(@args, -maxrepetitions =&gt; 25); 
		
		outer: while (defined($s-&gt;get_bulk_request(@args))) 
		{

			my @oids = oid_lex_sort(keys(%{$s-&gt;var_bind_list}));

			foreach (@oids) 
				{

					if (!oid_base_match($ifSer, $_)) { last outer; }
					#print("%s =&gt; %s\n", $_, $s-&gt;var_bind_list-&gt;{$_});
		
					# Make sure we have not hit the end of the MIB
					if ($s-&gt;var_bind_list-&gt;{$_} eq 'endOfMibView') { last outer; } 
				}
		
			# Get the last OBJECT IDENTIFIER in the returned list
			@args = (-maxrepetitions =&gt; 25, -varbindlist =&gt; [pop(@oids)]);
		}
 }
	
 	
 	#create the file with ALL entries
 	open OUTPUT, "&gt;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, "&lt;alllist.txt";
	my @data = grep { !/\S+;;/ } &lt;FINISH&gt;;
	open WRITER,"&gt;&gt;finallist.txt";
	print WRITER @data;
	close WRITER;
	close FINISH;


	#close the SNMP session
	$s-&gt;close();
}
#delete the alllist.txt, ipfile.txt and namefile.txt file
unlink 'alllist.txt','ipfile.txt','namefile.txt';

exit 0;


&lt;/code&gt;&lt;/readmore&gt;&lt;/p&gt;

&lt;sub&gt;&lt;p&gt;janitored by [ybiC]: Balanced &amp;lt;readmore&gt; tags around longish codeblock, as per Monastery convention&lt;/p&gt;&lt;/sub&gt;</field>
</data>
</node>
