SNMP 添加MIB 节点 (九) --- SNMP.CONF PASS添加节点

本文介绍了如何在Ubuntu系统中创建一个SNMP脚本,通过MIBOID调用并实现GET, GET NEXT, SET操作。步骤包括创建shell脚本、配置snmpd.conf,并演示了使用snmpget、snmpgetnext和snmpset命令的交互过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在/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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值