Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Extracting tagged data from a XML file

by theroninwins (Friar)
on Sep 01, 2004 at 14:31 UTC ( [id://387537]=note: print w/replies, xml ) Need Help??


in reply to Extracting tagged data from a XML file

OK I decided to try my luck with XML::Dumper and XML::Simple. They are easyly installed and I guess they just have to do. I got the code down to :
#!/usr/bin/perl -w # use strict; use XML::Simple; use Data::Dumper; my $xmlfile = "e:/topo.xml"; my $ref = eval { XMLin($xmlfile) }; if ($@){ print "XML Read ERROR"; } else { foreach my $item (@{$ref->{Layer2Details}}){ print $item->{IPAddresse}, "\n"; } }

and the Dumper gives this:
$VAR1 = { 'SchemaVersion' => '1.0', 'Layer2Details' => { 'Device' => [ { 'DeviceName' => 'LWL-H91-CW +-4-5-4.bc.de.bic', 'DeviceType' => 'C2950G-24' +, 'IPAddress' => '148.192.59. +254', 'Neighbors' => { 'Neighbor' = +> [

now I just have to know how to get over the Layer2Detail into the Device section. The rest I know. Any help on that?? Would really be great and thanks for taking that much time helping me already.

Replies are listed 'Best First'.
Re^2: Extracting tagged data from a XML file
by edan (Curate) on Sep 01, 2004 at 14:47 UTC
    foreach my $item ( @{$ref->{Layer2Details}{Device}} ) { print $item->{IPAddress}, "\n"; }
    --
    edan

      OK thanks for all of your help everyone and here it is the work I made all the tmie:
      #!/usr/bin/perl -w use Net::SNMP; use strict; use warnings; use diagnostics; use XML::Simple; use Data::Dumper; #open xml file my $xmlfile = "e:/topo.xml"; 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; } } #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, ">finallist.txt"; close ERASE; 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; } } #creat 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 FINOUT, ">>finallist.txt"; print FINOUT "$ip\n"; foreach my $hashref (@outlist) { print FINOUT "$hashref->{'file1'};$hashref->{'file2'}\n"; } print FINOUT "\n"; close FINOUT; } #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.
        There is a commented version in the snippets and CUFP section
      Thabnks I got it out myself as well after some lucky tries :-) BTW you must put a -> between the Layer2Detail and the Device.
        BTW you must put a -> between the Layer2Detail and the Device

        Actually, the arrow is optional in that position. You need the first one, since without it, Perl would interpret $ref{Layer2Details} to mean the value associated with the 'Layer2Details' key in %ref, rather than the value associated with the 'Layer2Details' key in hash referred to by $ref.

        All this and more is explained in Mark's very short tutorial about references which is part of your Perl distribution.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found