in reply to snmpwalk to CSV #2
Do the host names you show at the top correspond to the IP addresses shown in each of the snmpwalk runs? So you have something already that does the IP address lookup for the host name, and you want to run a set of 4 snmpwalk commands on each IP?
Start out with a hash whose keys are the IP addresses. Each element of this hash will be a reference to another hash, whose keys are the four types of information needed. For each type of info on a given host, there will be a reference to an array of port values. That is, your data structure will end up as a "hash of hashes of arrays" (HoHoA). Now, it's just a matter of looping over IP's and info types.
(Also, since there are -- or could be -- commas in the data, you'll want to quote the fields in the csv output that you create.)
Now, I think that will do what you want (it's untested), but I would have expected that including the IP address and maybe the port number on each line would be a good idea too -- if that's true, just change $outstr = ''; to $outstr = sprintf( "%s,%s,", $ip, $portid+1 );#!/usr/bin/perl use strict; # make sure we can open input and output files open( H, "host_list_file" ) or die "host_list_file: $!"; 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 = ... # 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 = "public interfaces.ifTable.ifEntry."; # UPDATED to includ +e 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 = ''; for my $info ( @infolist ) { $outstr .= '"' . $ipdata{$ip}{$info}[$portid] . '",'; } $outstr =~ s/,$/\n/; # replace trailing comma print O $outstr; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: snmpwalk to CSV #2
by getwithrob (Initiate) on Sep 23, 2005 at 15:24 UTC | |
by graff (Chancellor) on Sep 23, 2005 at 15:57 UTC | |
by getwithrob (Initiate) on Sep 23, 2005 at 17:22 UTC | |
by graff (Chancellor) on Sep 23, 2005 at 22:30 UTC | |
by getwithrob (Initiate) on Sep 23, 2005 at 17:13 UTC |