Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables for a script via SNMP by using snmpget?

Tags:

snmp

net-snmp

I have one simple bash script calls test_snmp, let's say:

#!/bin/bash
 echo $1

I have snmpd.conf set as following:

rwcommunity public 127.0.0.1
extend .1.3.6.1.4.1.2021.53 /bin/bash /tmp/test_snmp

What I'd like to do is to run a snmpwalk command, something like:

snmpwalk -v2c -c public 127.0.0.1 .1.3.6.1.4.1.2021.53 "PRINT SOMETHING"

from the output I see that oid = iso.3.6.1.4.1.2021.53.3.1.1.9.47.98.105.110.47.98.97.115.104 = "", is the output of the script. But I'd like to pass that string "PRINT SOMETHING", as a $1 parameter for the script mentioned above and then get the string (in this case "PRINT SOMETHING") by using snmpget command, something like:

snmpget -v2c -c public 127.0.0.1 iso.3.6.1.4.1.2021.53.3.1.1.9.47.98.105.110.47.98.97.115.104

It is only an example, I'm testing what options I have by running scripts via snmp, because if this works then I'll write another scripts to run remotely, but I have to run them with variables.

Does anyone know how to do it?

Thank you

like image 734
Severin Simko Avatar asked Oct 17 '22 22:10

Severin Simko


1 Answers

I spent lot of time before found all answers. I hope, it will be start point for someone and will save time.

/etc/snmp/snmpd.conf:

rwcommunity public
pass .1.3.6.1.4.1.YOUR_NUM_HERE.1 /path/to/your/script.sh

/path/to/your/script.sh:

#!/bin/bash 

case "$1" in
    -g) // GET Req
        echo $2 # ANSWER OID
        echo "string" # string/int/etc...
        echo "you data" #
    ;;
    -s)  // SET Req
    # Your code for processing SET Req
    exit 0
    ;;

    -n) // GETNEXT Req

        echo $2
        echo "string"
        # Your code for processing GETNEXT Req
    ;;
    *)
    ;;
esac

You can use snmpwalk after that and place your code after "-n" case. In case of snmpget, you code will be after "-g"

like image 179
Sergey NN Avatar answered Oct 21 '22 03:10

Sergey NN