SNMP execute remote script

In NET-SNMP it is easy to create options in the snmpd.conf file which will execute scripts.

This way it is easy to execute scripts from a remote server without the need to log in, or have another daemon running.

On the host, where the script should be executed, runs the snmp daemon that comes with the NET-SNMP package. In de file snmpd.conf we add the following line:

exec filedate /usr/local/bin/
exec filesize /usr/local/bin/

On this host we also create these scripts:
ls -l /etc/hosts | awk '{print $6,$7,$8}'
exit 0


ls -l /etc/hosts | awk '{print $5}'
exit 0

The oid which can be used to execute the scripts is: .

The next command can be executed on any remote host (which has the rights):

snmpwalk -v1 -c public .

The output would be something like this:

UCD-SNMP-MIB::extIndex.1 = INTEGER: 1
UCD-SNMP-MIB::extIndex.2 = INTEGER: 2
UCD-SNMP-MIB::extNames.1 = STRING: filedate
UCD-SNMP-MIB::extNames.2 = STRING: filesize
UCD-SNMP-MIB::extCommand.1 = STRING: /usr/local/bin/ 
UCD-SNMP-MIB::extCommand.2 = STRING: /usr/local/bin/ 
UCD-SNMP-MIB::extResult.1 = INTEGER: 0
UCD-SNMP-MIB::extResult.2 = INTEGER: 0
UCD-SNMP-MIB::extOutput.1 = STRING: nov 12 18:52
UCD-SNMP-MIB::extOutput.2 = STRING: 27851
UCD-SNMP-MIB::extErrFix.1 = INTEGER: 0
UCD-SNMP-MIB::extErrFix.2 = INTEGER: 0
UCD-SNMP-MIB::extErrFixCmd.1 = STRING: 
UCD-SNMP-MIB::extErrFixCmd.2 = STRING:

Now you see there is an extra oid available which will just give the outcome of the script.

snmpwalk -v1 -c public UCD-SNMP-MIB::extOutput.1
UCD-SNMP-MIB::extOutput.2 = STRING: 27851

In Nagios, you could use the check_snmp plugin:

check_snmp -H -o UCD-SNMP-MIB::extOutput.2 -s 27851
SNMP OK - 27851 | UCD-SNMP-MIB::extOutput.2=27851