Hello,
I'm writing a bash script to keep ethernet switch counters under control via SNMP queries.
At some point this script connects via snmpwalk:
Code:
# Get array port index
arrIndex=(`snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1 | awk '{print $4;}'`)
# Is it possibile to get answers from switch?
# The exit state of the last command is evaluated '0' if True or '1' if False and is intercepted by '$?'
if [ $? -ne 0 ]; then
# Something's wrong
echo "Wrong IP address or SNMP not configured for switch."
exit $EXIT_UNKNOWN
fi
If the switch IP is wrong or if SNMP on the switch is not configured I got:
Code:
Timeout: No Response from 10.68.254.245
How can I handle this exception in the script?
The code above doesn't work, because I always get
'0' exit code.
I noticed that:
Quote:
arrIndex=(`snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1 | awk '{print $4;}'`)
|
and
Quote:
snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1 | awk '{print $4;}'
|
exit with
'0'.
Only
Code:
snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1
exits with
'1' in case the IP is wrong.
I would prefer not to do a pre-flight check by repeating the command snmpwalk, because that would take extra time.
So the question is: how can I handle the exit code generated by the command snmpwalk above, which is then piped and then assigned to an array?
Thanks.
--
rob