LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash and snmpget (https://www.linuxquestions.org/questions/programming-9/bash-and-snmpget-714113/)

seefor 03-24-2009 01:35 PM

Bash and snmpget
 
Thanks for even looking at this :)

I have a SNMP script that pull 3 OIDs from a device:
Code:

snmpget -v 1 -Ovq -r 0 -c public 10.75.131.181 .1.3.6.1.4.1.11268.2.1.1.2.29.1.1.4.3.1 .1.3.6.1.4.1.11268.2.1.1.2.29.1.1.4.8.1 .1.3.6.1.4.1.11268.2.1.1.2.29.1.1.4.11.1 |  sed -n '1h;2,$H;${g;s/\n/,/g;p}' >> test
When I run the script I get the following:
Quote:

Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: SNMPv2-SMI::enterprises.11268.2.1.1.2.29.1.1.4.11.1
Code:

# cat test
15657,2466

How can I set it to output the error to the file?

Example :

Code:

# cat test
15657,2466,NA

Thanks in advance for you help or suggestion.
SeeFor

PTrenholme 04-01-2009 01:00 PM

I suspect that the error message is being written to /dev/stderr, and all you'd need to do is capture the message to a file (e.g., 2>/tmp/errors or the like), and then parse the error(s).

Note: Just guessing because nobody more knowledgeable than I has yet replied to your inquiry.

dwhitney67 04-01-2009 01:16 PM

Maybe after running the snmpget, the shell's status is set to a non-zero value to indicate an error; thus perhaps $? can be used. I'm not sure though if the sed will affect this value or not.

Code:

sed_res=`snmpget -v 1 -Ovq -r 0 -c public 10.75.131.181 .1.3.6.1.4.1.11268.2.1.1.2.29.1.1.4.3.1 .1.3.6.1.4.1.11268.2.1.1.2.29.1.1.4.8.1 .1.3.6.1.4.1.11268.2.1.1.2.29.1.1.4.11.1 | sed -n '1h;2,$H;${g;s/\n/,/g;p}'`

if [ $? -eq 0 ]
then
        echo "$sed_res" >> test
else
        echo "$sed_res,NA" >> test
fi



All times are GMT -5. The time now is 12:06 AM.