With Nagios, I want to read a device name of hundreds of Ubiquity devices. This value has one OID on some of them and another on others. The best way I got so far is using this script:

$1/check_snmp -H $2 -o .1.2.840.10036. > /tmp/$2.snmp.nagios.tmp
if [ $status -ne 0 ]; then
  $1/check_snmp -H $2 -o .1.2.840.10036. > /tmp/$2.snmp.nagios.tmp
cat /tmp/$2.snmp.nagios.tmp
exit $status

What I don't like here is that it uses filesystem, but I don't know how to read output of a command to one variable and exit code of the command to another variable.

Is there a way to write this so it uses variables only?

Isn't there a better way to achive the main goal?

2 Answers 2


You can use directly the command snmpget instead check_snmp because check_snmp will use snmpget (or other tool of netsnmp tools) and works on the result stored in a variable.

I paste here the skeleton of a script I use for that :

function control_error {
    echo "UNKNOWN : Error during operation" 
    exit 3
drive="$(snmpget -v1 -Ovq -c <community snmpv1> $IPadress .$i)"
if [ $? != 0 ]; then control_error;fi
drive="$(echo $drive | sed -e 's/"//g')"

This script read an OID and store the result in the variable drive.

it tests the evental error of snmpget and return 3 (unknown for nagios) if problem.

The snmpget options are :

  • -v1 version of snmp
  • -c community
  • -Ovq OUPUT options which returns only the result without the asked OID

After you can work on your variable for example in this script with echo and sed


I usually do it like this:

data=$(your command)

This way you get the output of your command in $data and the return code in $result. But maybe there is a much nicer way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .