Yep, I'm that guy that stinks when it comes to programming..... I posted last week and was able to get a script to do what I needed and for that I am very grateful! However, I wasn't able to get the hostnames to display in the first field instead of IP addresses. I tried a couple of things the gentlemen suggested but I couldn't get it to work. Also, I need to walk a field that's not in the same part of the MIB structure and wasn't able to add it in and make it work. The 4 things I originally requested ifDescr, ifAdminStatus, ifOperStatus, ifLastChange are all in the interfaces.ifTable.ifEntry portion of the MIB structure. I see how to add something that's in the same part of the tree like ifSpeed and ifPhysAddress.
interfaces.ifTable.ifEntry.ifDescr interfaces.ifTable.ifEntry.ifAdminStatus interfaces.ifTable.ifEntry.ifOperStatus interfaces.ifTable.ifEntry.ifLastChange interfaces.ifTable.ifEntry.ifSpeed interfaces.ifTable.ifEntry.ifPhysAddress
But I don’t see how to add something that’s in a different portion of the tree like:
ifMIB.ifMIBObjects.ifXTable.ifXEntry.ifAlias or system.sysUpTime or system.sysName or udp.udpTable.udpEntry.udpLocalAddress or icmp.icmpOutDestUnreachs.
Here’s the code I was given. Help is greatly appreciated!
#!/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; } }

In reply to SNMP Walk to CSV file - 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.