getwithrob has asked for the wisdom of the Perl Monks concerning the following question:
But I don’t see how to add something that’s in a different portion of the tree like:interfaces.ifTable.ifEntry.ifDescr interfaces.ifTable.ifEntry.ifAdminStatus interfaces.ifTable.ifEntry.ifOperStatus interfaces.ifTable.ifEntry.ifLastChange interfaces.ifTable.ifEntry.ifSpeed interfaces.ifTable.ifEntry.ifPhysAddress
Here’s the code I was given. Help is greatly appreciated!ifMIB.ifMIBObjects.ifXTable.ifXEntry.ifAlias or system.sysUpTime or system.sysName or udp.udpTable.udpEntry.udpLocalAddress or icmp.icmpOutDestUnreachs.
#!/usr/bin/perl use strict; # make sure we can open input and output files open( H, "/opt/home/johnsonr/scripts/nodes" ) or die "/opt/home/johnso +nr/scripts/nodes: $!"; open( O, ">output.csv" ) or die "output.csv: $!"; # read host list my @hosts = <H>; close H; chomp @hosts; # remove "\r\n" from ends of lines my @iplist; for my $host ( @hosts ) { my $ip = `getent hosts $host`; $ip =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/s; # do whatever you do to get $ip for $host (and chomp if needed) push @iplist, $ip; } my %ipdata; my @infolist = qw/ifDescr ifOperStatus ifAdminStatus ifLastChange/; my $cmdarg = "VER143r interfaces.ifTable.ifEntry."; # UPDATED to inclu +de final "." for my $ip ( @iplist ) { for my $info ( @infolist ) { my @pdata = `snmpwalk $ip $cmdarg$info`; chomp @pdata; # remove line-terminations s/.* = // for (@pdata); # remove redundant text $ipdata{$ip}{$info} = [ @pdata ]; # create array ref in the ha +sh structure $ipdata{$ip}{lastport} = $#pdata; # keep track of how many por +ts } } # now print the csv data for my $ip ( @iplist ) { for my $portid ( 0 .. $ipdata{$ip}{lastport} ) { my $outstr = sprintf ( "%s,%s,", $ip, $portid+1 ); for my $info ( @infolist ) { $outstr .= '"' . $ipdata{$ip}{$info}[$portid] . '",'; } $outstr =~ s/,$/\n/; # replace trailing comma print O $outstr; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: SNMP Walk to CSV file -
by McDarren (Abbot) on Sep 29, 2005 at 11:50 UTC |