Description: |
The program extracts IPAddress tagged data from a xml file, saves them in a txt file. Then it uses that data to get more information for each device via SNMP. It saves all of these infos in different txt files and then generates one resultfile where all informations are sorted per device in a cvs file style so it can be used to automatically be read by programs like CiscoWorks 2000 to update device informations. in the end it parses the results and deltes incomplet lines.
UPDATED 06.09.04 |
#!/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 and a name file with one ip
+address per line. #
#It reads the IPAddress ansd the DeviceName tagged information from th
+e xml file it parses.#
#The IP addresses and device names are then saved in a txt file.
+ #
#The information saved here are later used and referred to as ipfile.t
+xt and namefile.txt #
#Ver:1.0 written by :Ronin #
######################################################################
+######################
#open xml file
my $xmlfile = $ARGV[0];
my $ref = eval { XMLin($xmlfile) };
#erase or creat the ipfile and the namefile
open ERASER, ">ipfile.txt";
close ERASER;
open ERASER, ">namefile.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 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 including the name file 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 ERASER;
open ERASER, ">desclist.txt";
close ERASER;
open ERASER, ">finallist.txt";
close ERASER;
#looping as long as there are IP addersses
my %record;
#my $count;
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");
open(OUT3, "namefile.txt");
my @outlist;
while (<OUT>)
{
my %hash;
my @temp = split(/;/,$_);
$hash{'file1'} = $temp[0];
$hash{'file2'} = <OUT2>;
$hash{'file3'} = <OUT3>;
#delete Return
foreach (values %hash)
{
$_ =~ s/\n//gi;
}
push(@outlist,\%hash);
$record{$hash{file1}} = \%hash if $hash{file1};
}
#print to file
open ALLOUT, ">finallist.txt";
foreach my $hashref (values %record)
{
print ALLOUT "$hashref->{'file3'};$hashref->{'file1'};$has
+href->{'file2'}}\n";
}
close ALLOUT;
#\@outlist, \%record;
close(OUT);
#open the file
# open SCAN, "<alllist.txt";
#Search for pattern ;; and if found ignore that line
# my @data = grep { !/\S+;;/ } <SCAN>;
#write the rest to a file
# open FINOUT, ">>finallist.txt";
# print FINOUT @data, "\n";
#$/ = "$data";
#while (<$data>)
#{
# my %seen;
# my @output= grep !$seen{$_}++, split (/^/m, $_);
# #print FINOUT @output;
# }
# close SCAN;
}
#close session
$session->close();
#last if ++$count > 20;
}
Re: Extracting xml data and using them for SNMP
by jeffa (Bishop) on Sep 02, 2004 at 17:38 UTC
|
That's a LOT of code for what seems to be such a minor task. All those files you open and close, do you
really need them? Wouldn't simple arrays and hashes and perhaps some Caching be better? I will admit
that when i first started Perl, i had a bad habit of looping many times instead of just once or twice.
Unrelated to that, one particular item that really bothers me is this:
my $ref = eval { XMLin($xmlfile) };
#see if open worked
if ($@) {
print "XML Read ERROR";
}
else { ... }
Why are you bypassing the errors reported from XML::Simple in this manner? Seems to me that if the file cannot be parsed then you don't need to be continuing execution. Not to mention that XML::Simple will
return meaningful messages as to why the file did not parse. By trapping the die you have no clue
as to what went wrong.
Finally, unless you provide some example XML, we can't really try your code out. But i still think you
could trim this down to under 40 lines. ;)
| [reply] [d/l] |
|
<?xml version="1.0" encoding="UTF-8" ?>
- <CMData>
<CMServer>BIBAS03</CMServer>
<CreatedAt>Tue Aug 24 09:09:41 GMT+02:00 2004</CreatedAt>
<SchemaVersion>1.0</SchemaVersion>
<Heading>Topology Data</Heading>
- <Layer2Details>
- <Device>
<DeviceName>LWL-H91-CW-4-5-4.bc.de.bic</DeviceName>
<IPAddress>148.192.59.254</IPAddress>
<DeviceState>Reachable</DeviceState>
<DeviceType>C2950G-24</DeviceType>
- <Neighbors>
- <Neighbor>
<NeighborIPAddress>148.192.22.22</NeighborIPAddress>
<NeighborDeviceType>C6506</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Gi0/2</LocalPort>
<RemotePort>3/3</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.22.18</NeighborIPAddress>
<NeighborDeviceType>C6509</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Gi0/1</LocalPort>
<RemotePort>3/3</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.59.253</NeighborIPAddress>
<NeighborDeviceType>C2950-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/1</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.59.252</NeighborIPAddress>
<NeighborDeviceType>C2950-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/2</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
</Neighbors>
</Device>
- <Device>
<DeviceName>stp-g91-cw-2-1-3.bc.de.bic</DeviceName>
<IPAddress>148.192.166.249</IPAddress>
<DeviceState>Reachable</DeviceState>
<DeviceType>C2950-24</DeviceType>
- <Neighbors>
- <Neighbor>
<NeighborIPAddress>148.192.166.254</NeighborIPAddress>
<NeighborDeviceType>C2950G-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/1</LocalPort>
<RemotePort>Fa0/3</RemotePort>
</Neighbor>
</Neighbors>
</Device>
- <Device>
<DeviceName>stp-k41-cw-e-2-2.bc.de.bic</DeviceName>
<IPAddress>148.192.86.253</IPAddress>
<DeviceState>Reachable</DeviceState>
<DeviceType>C2924XLV</DeviceType>
- <Neighbors>
- <Neighbor>
<NeighborIPAddress>148.192.86.254</NeighborIPAddress>
<NeighborDeviceType>C2950G-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/1</LocalPort>
<RemotePort>Fa0/4</RemotePort>
</Neighbor>
</Neighbors>
</Device>
- <Device>
<DeviceName>stp-j51-cw-1-2-2.bc.de.bic</DeviceName>
<IPAddress>148.192.88.253</IPAddress>
<DeviceState>Reachable</DeviceState>
<DeviceType>C2950-24</DeviceType>
- <Neighbors>
- <Neighbor>
<NeighborIPAddress>148.192.88.254</NeighborIPAddress>
<NeighborDeviceType>C2950G-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/1</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
</Neighbors>
</Device>
- <Device>
<DeviceName>lwl-h81-cw-u-1-1.bc.de.bic</DeviceName>
<IPAddress>148.192.74.254</IPAddress>
<DeviceState>Reachable</DeviceState>
<DeviceType>C2950G-24</DeviceType>
- <Neighbors>
- <Neighbor>
<NeighborIPAddress>148.192.74.251</NeighborIPAddress>
<NeighborDeviceType>C2950-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/13</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.22.14</NeighborIPAddress>
<NeighborDeviceType>C6506</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Gi0/2</LocalPort>
<RemotePort>4/1</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.22.10</NeighborIPAddress>
<NeighborDeviceType>C6506</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Gi0/1</LocalPort>
<RemotePort>4/1</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.74.249</NeighborIPAddress>
<NeighborDeviceType>C2950-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/6</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.74.250</NeighborIPAddress>
<NeighborDeviceType>C2950-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/5</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
- <Neighbor>
<NeighborIPAddress>148.192.74.253</NeighborIPAddress>
<NeighborDeviceType>C2950C-24</NeighborDeviceType>
<Link>Point to Point link</Link>
<LocalPort>Fa0/24</LocalPort>
<RemotePort>Fa0/1</RemotePort>
</Neighbor>
</Neighbors>
</Device>
</Layer2Details>
</CMData>
Here is some XML to try the code with sorry about not posting it before | [reply] [d/l] |
|