首先在/home下建个shell脚本passtest
---------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
echo $2 #因为snmpd是用EXEC -g MIBOID来调脚本的,所以MIBOID是第二个参数
echo string
echo "hello world"
---------------------------------------------------------------------------------------------------------------------------------
随后在snmod.conf中添加
---------------------------------------------------------------------------------------------------------------------------------
pass .1.3.6.1.4.1.8072.2.255 /bin/sh /home/passtest
---------------------------------------------------------------------------------------------------------------------------------
之后,执行ubuntu@ubuntu:/usr/local/net-snmp/sbin$ sudo ./snmpd -f -Le
管理端执行如下:
ubuntu@ubuntu:/usr/local/net-snmp/bin$ snmpget -v 2c -c public localhost 1.3.6.1.4.1.8072.2.255
NET-SNMP-PASS-MIB::netSnmpPassExamples = STRING: "hello world"
ubuntu@ubuntu:/usr/local/net-snmp/bin$
添加get,get-next,set
修改脚本内容如下
---------------------------------------------------------------------------------------------------------------------------------
bin/sh
opt=$1
oid=$2
if [ "$opt" = "-g" ]; then
#do_the_snmpget
#...
echo $oid
echo string
echo "get operator"
elif [ "$opt" = "-n" ]; then
#do_the_snmpgetnext
#...
echo $oid
echo string
echo "get next operator"
elif [ "$opt" = "-s" ]; then
#do_the_snmpset
#...
if_success, exit_directly
if_fail, echo not-writable or wrong-type
fi
exit 0
---------------------------------------------------------------------------------------------------------------------------------
执行ubuntu@ubuntu:/usr/local/net-snmp/sbin$ sudo ./snmpd -f -Le
管理端执行如下:
ubuntu@ubuntu:/usr/local/net-snmp/bin$ snmpget -v 2c -c public localhost 1.3.6.1.4.1.8072.2.255
NET-SNMP-PASS-MIB::netSnmpPassExamples = STRING: "get operator"
ubuntu@ubuntu:/usr/local/net-snmp/bin$ snmpgetnext -v 2c -c public localhost 1.3.6.1.4.1.8072.2.255
NET-SNMP-PASS-MIB::netSnmpPassExamples = STRING: "get next operator"
ubuntu@ubuntu:/usr/local/net-snmp/bin$ snmpset -v 2c -c public localhost 1.3.6.1.4.1.8072.2.255 s "hello world"
NET-SNMP-PASS-MIB::netSnmpPassExamples = STRING: "hello world"
ubuntu@ubuntu:/usr/local/net-snmp/bin$ snmpget -v 2c -c public localhost 1.3.6.1.4.1.8072.2.255
NET-SNMP-PASS-MIB::netSnmpPassExamples = STRING: "get operator"
ubuntu@ubuntu:/usr/local/net-snmp/bin$
修改
---------------------------------------------------------------------------------------------------------------------------------
elif [ "$opt" = "-s" ]; then
#do_the_snmpset
#...
#if_success, echo not-writable or wrong-type
#if_fail, echo not-writable or wrong-type
echo $* >> /tmp/passtest.log
exit 0
fi
---------------------------------------------------------------------------------------------------------------------------------
执行ubuntu@ubuntu:/usr/local/net-snmp/sbin$ sudo ./snmpd -f -Le
管理端执行如下:ubuntu@ubuntu:/usr/local/net-snmp/bin$ snmpset -v 2c -c public localhost 1.3.6.1.4.1.8072.2.255 s "hello world"
NET-SNMP-PASS-MIB::netSnmpPassExamples = STRING: "hello world"
之后 vi /tmp/passtest.log
可以看到
-s .1.3.6.1.4.1.8072.2.255 string hello world
$1 $2 $3 $4
PROG -g MIBOID — g 代表 get
PROG -n MIBOID — n 代表 getnext
PROG -s MIBOID TYPE VALUE — s 代表 set
模板来自网上,如下
#!bin/bash
opt=$1
oid=$2
if [ "$opt" = "-g" ]; then
do_the_snmpget
...
echo $oid
echo TYPE
echo VALUE
elif [ "$opt" = "-n" ]; then
do_the_snmpgetnext
...
echo $oid
echo TYPE
echo VALUE
elif [ "$opt" = "-s" ]; then
do_the_snmpset
...
if_success, exit_directly
if_fail, echo not-writable or wrong-type
fi
exit 0