I was given a perl script that will read Cisco device hostnames from a text file then snmp query different values against those devices and write the output to a .csv file. The values I want to query are ifDescr, ifOperStatus, ifAdminStatus, ifLastChange. See the “my @columns” section in the first line or two. Here's the problem I have. The script appears to walk all values but only writes the first row of output to the .csv file. Can someone help me out here? Oh yea. My programming skills are weak at best…..especially in perl!
#!/usr/bin/perl my @columns = qw/interfaces.ifTable.ifEntry.ifDescr interfaces.ifTable +.ifEntry.ifAdminStatus interfaces.ifTable.ifEntry.ifOperStatus interf +aces.ifTable.ifEntry.ifLastChange/; my $CSV = "/opt/home/johnsonr/scripts/output.csv"; my $NODES = "/opt/home/johnsonr/scripts/nodes"; # use strict 'vars'; use Getopt::Std; use vars qw/ %opt /; my (%snmpwalk); my ($csv, $nodes, $tmp, $i, $x, $nam, $val, $node); my ($opt_d, $opt_c, $opt_n, $debug, $column, $node, $line); getopts ('dc:n:',\%opt) or &usage; $debug = $opt{d} ? 1 : 0; $csv = $opt{c} ? $opt{c} : $CSV; $nodes = $opt{n} ? $opt{n} : $NODES; print "nodes=$nodes\n"; sub usage() { print STDERR << "EOF"; This program read a nodes files and creates a csv file with snmp v +alues for each node. usage: $0 [-d] [-c:csv_file] [-n:nodes_file] [test-input-file-jus +t-for-demo] -d : print debugging messages to stderr -c : csv output file name. Default $CSV -n : nodes input file name. Default $NODES EOF exit; } open (NODES, "<$nodes") or die "could not open nodes file $nodes"; + open (CSV, ">$csv") or die "could not open scv file $csv"; + $tmp = join(",",@columns); print CSV "$tmp\n"; while (<NODES>) { chop; $node = $_; $line = ""; print "- $node\n" if $debug; foreach $column (@columns) { $_ = `snmpwalk $node VER143r $column`; if (/=\s*(.*)$/) { $val = $1; } else { $val = "???"; } $line .= $val . ","; print "-- $column, $val\n" if $debug; } chop $line; print CSV $line."\n"; }

2005-09-23 Retitled by g0n, as per Monastery guidelines
Original title: 'Can someone help???'


In reply to Writing 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.