0

if it possible to do a snmpwalk in a perl script and put the output in a table to make a sort of association like for each hostname i have in the same line if index and desc i have the script in bash but the output that i have don't give the association that i want so i need your help

#!/bin/bash
Rep_Scripts='/home/scripts'
out_file='/home/scripts/out_file'
rm -rf $Rep_Scripts/out_file

for i in `cat $Rep_Scripts/IP_ALU_LIST.txt | awk '{print}'`
do
read hostname ip <<< $(echo $i |sed 's/;/ /')


        echo "${hostname} ==> ${ip} If_Name" >> out_file
        snmpwalk -v2c -c ${ip} OID>> out_file
        echo "${hostname} ==> ${ip} Global_If_Index" >> out_file
         snmpwalk -v2c -c ${ip} OID >>  out_file
        echo "${hostname} ==> ${ip} If_Statut" >> out_file
        snmpwalk -v2c -c  ${ip} OID >> out_file


done

i have in IP_ALU_LIST.txt

router2;89.100.12.100
router3;100.100.100.100
1
  • 2
    Punctuation: please learn to use it. Commented Apr 24, 2015 at 9:02

1 Answer 1

1

First, refactoring your question:

How can I construct a perl script that inputs data from snmpwalk for each IP/Host and outputs a table for each OID.

Second, your example snmpwalk commands make no sense. It might make sense if OID were a variable. You problably mean to use snmpwalk -v2 -c public ${ip} ${OID}

You could do something like this:

#!/usr/bin/env perl -w
# Invoke via $0 <ip-address-file> <OIDs ... >
$input_file=shift @ARGV;
$oids=join(" ",@ARGV);
$oids gt '' || die "Please provide at least one OID for snmpget to fetch"

format STDOUT_TOP = 
@<<<<<<<<<<<  @>>>>>>>>>>>>>>>>>>>
$ip,$hostname
.
format STDOUT = 
@<<<<<<<<<<<<<<<<<<<<< @||||||||||||| @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$oid,$type,$value
.
$^L="\n\n\n";

# parse inputfile
open(IPFILE,$input_file) or die "Cannot open $input_file";
while (my $host=<IPFILE>) { 
  chomp $host;
  my ($hostname,$ip) = split(";",$host,2);

  open(SNMP,"snmpget -v2 -cpublic $ip $oids |") or die "Cannot run snmpwalk"
  print "$hostname => $ip :\n";
  while ($_ = <SNMP>) { 
       if ( ($oid,$type,$value) = /^(.*?) = ([^:]+): (.*)$/ ) { 
          write
       } else {
          print STDERR  "Could not parse this output:\n  $_";
       }
  }
  $- = 0;
}

snmpget will output one line for each OID. See http://linux.die.net/man/1/perlform for more on the Perl "format" and the associated write command. The $^L sets the page-delimiter as three line-feeds instead of the traditional page-eject character (which is CTRL-L). The $- = 0; bit essentially forces a new page (with a new header) the next time "write" is called. And the next time it's called, $hostname and $ip will have new values.

3
  • i have different OID i have three ( description,ifname,ifindex) pliz look to the output that i want following to this link :unix.stackexchange.com/questions/198137/… Commented Apr 24, 2015 at 13:06
  • OK, I updated the answer to reflect more of what I think you want. My answer uses the traditional format...write functionality of perl (which was there since the 1980s!). Commented Apr 24, 2015 at 15:43
  • i am trying to execute the script i have an error ** line 26, near "print"** so i think the ipfile is unreachable how i can put the path for txt file to take it this is what i put in the script :$input_file= "/home/scripts/IP_ALU_LIST.txt" thankyou for your help Commented Apr 27, 2015 at 9:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.