- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- snmp script question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 12:21 PM
09-20-2005 12:21 PM
johnsonr@nmscrme01 > cat snmptest2
index=0
for device in `cat /opt/home/johnsonr/scripts/nodes`
do
snmpwalk $device public system.sysName.0
ret1[$index]=$?","
snmpwalk $device public interfaces.ifTable.ifEntry.ifDescr.1
ret2[$index]=$?","
snmpwalk $device public interfaces.ifTable.ifEntry.ifAdminStatus.1
ret3[$index]=$?","
snmpwalk $device public interfaces.ifTable.ifEntry.ifOperStatus.1
ret4[$index]=$?","
let index=index+1
done
johnsonr@nmscrme01 > ./snmptest2
system.sysName.0 = hostname.domain.com
interfaces.ifTable.ifEntry.ifDescr.1 = FastEthernet0/1
interfaces.ifTable.ifEntry.ifAdminStatus.1 = up(1)
interfaces.ifTable.ifEntry.ifOperStatus.1 = down(2)
johnsonr@nmscrme01 >
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 12:41 PM
09-20-2005 12:41 PM
SolutionThis noe topic appears to be a continuation of topic:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=958181
If you could attach a txt file with sample input and perhaps a few lines of sample output then more people will be able to try to help you.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 01:05 PM
09-20-2005 01:05 PM
Re: snmp script question
A file called nodes. See the second line in the snmptest2 script above.
In the nodes file is one line of text that is this:
lca-sfa-rtr1.somewhere.com
Additional device names can be added to this nodes file once I get it working.
The output of the script is:
johnsonr@nmscrme01 > ./snmptest2
system.sysName.0=lca-sfa-rtr1.somewhere.com interfaces.ifTable.ifEntry.ifDescr.1=Fa0/1
interfaces.ifTable.ifEntry.ifAdminStatus.1=up
interfaces.ifTable.ifEntry.ifOperStatus.1=down
johnsonr@nmscrme01 >
I would like to have a comma at the end of each of these lines so I can easily open the file as a comma seperated value file using MS excel.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 03:19 PM
09-20-2005 03:19 PM
Re: snmp script question
Ok, I have something to get you going, based on a similar problem I recently solved for myself.
The PERL script test.pl code is below, and in the attachment.
It is completely dynamic.
It reads the list of nodes from a file you can optionally specify defaulting to nodes.txt
It readd the list of columns from a variable you can readily edit.
It stores output in a file you can optionally specify defaulting to test.csv.
Hope this helps,
Enjoy,
Hein.
I can not do that 'snmpwalk' function on my system, so i replaced it with input data from a file. The test inputfile looked like:
------------------------- sample snmpdata.txt ------------
system.sysName.0=lca-sfa-rtr1.somewhere.com
interfaces.ifTable.ifEntry.ifDescr.1=Fa0/1
interfaces.ifTable.ifEntry.ifAdminStatus.1=up
interfaces.ifTable.ifEntry.ifOperStatus.1=down
system.sysName.0=lca-sfa-rtr1.elsewhere.com
interfaces.ifTable.ifEntry.ifDescr.1=Fa0/test
interfaces.ifTable.ifEntry.ifAdminStatus.1=left
interfaces.ifTable.ifEntry.ifOperStatus.1=right
-------------------------- sample nodes.txt --------------------------
lca-sfa-rtr1.somewhere.com
lca-sfa-rtr1.elsewhere.com
----- test result -----------------
C:\sap>perl test.pl < snmpdata.txt
C:\sap>type test.csv
system.sysName.0,interfaces.ifTable.ifEntry.ifDescr.1,interfaces.ifTable.ifEntr
.ifAdminStatus.1,interfaces.ifTable.ifEntry.ifOperStatus.1
lca-sfa-rtr1.somewhere.com,Fa0/1,up,down
lca-sfa-rtr1.elsewhere.com,Fa0/test,left,right
my @columns = qw/system.sysName.0 interfaces.ifTable.ifEntry.ifDescr.1 interfaces.ifTable.ifEntry.ifAdminStatus.1 interfaces.ifTable.ifEntry.ifOperStatus.1/;
my $CSV = "test.csv";
my $NODES = "nodes.txt";
#
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;
sub usage()
{
print STDERR << "EOF";
This program read a nodes files and creates a csv file with snmp values for each node.
usage: $0 [-d] [-c:csv_file] [-n:nodes_file] [test-input-file-just-for-demo]
-d : print debugging messages to stderr
-c : csv output file name. Default $CSV
-n : nodes input file name. Default $NODES
EOF
exit;
}
#
# temporary code to replace 'snmpwalk' which I do not have.
# just fakes it with an array read from STDIN
#
while (<>) {
chop;
($nam,$val)=split /=/;
$node = $val if (/sysName/);
$snmpwalk{$node.$nam}=$val;
print "$node|$nam|$val\n" if $debug;
}
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 (
chop;
$node = $_;
$line = "";
print "- $node\n" if $debug;
foreach $column (@columns) {
$val = $snmpwalk{$node.$column}; # replace with snmpwalk code
$line .= $val . ",";
print "-- $column, $val\n" if $debug;
}
chop $line;
print CSV $line."\n";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 05:44 PM
09-20-2005 05:44 PM
Re: snmp script question
index=0
for file in `ls`
do
ls -l $file >/dev/null
arr[$index]=$?","
let index=index+1
done
echo ${arr[*]}
index=0
for device in `cat /opt/home/johnsonr/scripts/nodes`
do
ret1[$index]=$(snmpwalk $device public system.sysName.0)
ret2[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifDescr.1)
ret3[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifAdminStatus.1)
ret4[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifOperStatus.1)
let index=index+1
done
i=0
while [[ $i -lt ${#ret1[*]} ]]
do
echo ${ret1[$i]},${ret2[$i]},${ret3[$i]},${ret4[$i]}
let i=i+1
done
### Problem is, $? is used to get Return code not the strings returned from there. It will work now. ###
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 11:49 PM
09-20-2005 11:49 PM
Re: snmp script question
I can walk a single device but when I tried to run the script you provided against the same device, I get an snmp timeout.
Please take a look.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 11:56 PM
09-20-2005 11:56 PM
Re: snmp script question
How will check reachablity to lcacptc23.cpt.ca.kp.org machine. Check like with ping as,
#!/usr/bin/ksh
ping lcacptc23.cpt.ca.kp.org -n 1 1>/dev/null 2>&1
if [[ $? -ne 0 ]]
then
echo "It is not reachable to lcacptc23.cpt.ca.kp.org"
exit 1
fi
index=0
for device in `cat /opt/home/johnsonr/scripts/nodes`
do
ret1[$index]=$(snmpwalk $device public system.sysName.0)
ret2[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifDescr.1)
ret3[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifAdminStatus.1)
ret4[$index]=$(snmpwalk $device public interfaces.ifTable.ifEntry.ifOperStatus.1)
let index=index+1
done
i=0
while [[ $i -lt ${#ret1[*]} ]]
do
echo ${ret1[$i]},${ret2[$i]},${ret3[$i]},${ret4[$i]}
let i=i+1
done
Execute this script and let us know.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2005 11:59 PM
09-20-2005 11:59 PM
Re: snmp script question
http://www.ibr.cs.tu-bs.de/pipermail/jasmin/2002-May/000522.html
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2005 12:15 AM
09-21-2005 12:15 AM
Re: snmp script question
Rob,
Did you give the perl code a try?
I think all you need to do is to replace the dummy snmpwalk with the real one:
$_ = `snmpwalk $node public $column`;
if (/=\s*(.*)$/) {
$val = $1;
} else {
$val = "???";
}
This also allows for spaces after the "=" in the snmpwalk output which I now see in the actual data lines in your attached text file.
Modifies, but untested, code attached.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2005 02:40 AM
09-21-2005 02:40 AM
Re: snmp script question
The perl script worked too. I actually like it better because it writes the output directly to a .csv file. That's the next thing I was going to request in the original script provided.
That's why I love this forum...when I need something like this, I always come away with a solution.
Thanks for you guys' help on this.