http://www.perlmonks.org?node_id=392342


in reply to Global vars not accessable globaly

Ok the prog is now running :
#!/usr/bin/perl -w 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); #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; 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; } } #open ipfile open IPFILE, "ipfile2.txt" or die "Can't get IPs - $!\n"; open NAMES, "namefile2.txt" or die "Can't get IPs - $!\n"; 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'; while ( $ip = <IPFILE>, $name = <NAMES> ) { chomp $ip; chomp $name; $name =~ s/\.bc\.de\.bic$//; 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!"); } my @args = ( -varbindlist => [$ifSer] ); my @args2 = ( -varbindlist => [$ifPart] ); my @args3 = ( -varbindlist => [$ifDesc] ); if ($s->version == SNMP_VERSION_1) { 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 => [$_]); } } 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') { l +ast outer; } } # Get the last OBJECT IDENTIFIER in the returned list @args = (-maxrepetitions => 25, -varbindlist => [pop(@oids +)]); } } open OUTPUT, ">alllist.txt"; for my $i (0 .. $#final1) { print OUTPUT "$ip;$name;$final1[$i];$ +final2[$i];$final3[$i]\n" } close OUTPUT; @final1=(); @final2=(); @final3=(); open FINISH, "<alllist.txt"; my @data = grep { !/\S+;;/ } <FINISH>; open WRITER,">>finallist.txt"; print WRITER @data; close WRITER; close FINISH; # Let the user know about any errors #if ($s->error() ne '') { # _exit($s->error()); #} # Close the session #$s->close(); } exit 0;
The prog gets the serial n/o, part name and the describtion of each part and lists it in finallist.txt like:

ip;name;serial1;part1;desc1 ip;name;serial2;part2;desc2 ip;name;serial3;part3;desc3
after reading the parsing the ips and names from a xml file.