If I knew more about snmpwalk and related tools, I would suggest that you find a way to get the same amount of information using fewer runs (e.g. get all information for a given host in a single snmpwalk command execution per host). But since I don't know enough to suggest that...

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.)

#!/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; } }
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 );

In reply to Re: snmpwalk to CSV #2 by graff
in thread snmpwalk to CSV #2 by getwithrob

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.