当前位置: 首页 > 工具软件 > NetSet > 使用案例 >

SNMP-SET

别帅
2023-12-01

基本示例

snmpset命令的语法类似于该命令的语法snmpget ,并且大部分snmpget 教程也适用于此。主要区别在于指定要使用的信息。该snmpset命令需要更新的 OID、此对象的数据类型以及要应用的新值,而不是单个 OID 

snmpset -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0 s "hi there!"

这个命令的效果通常可以通过在 SET 请求之前和之后检索对象的值来看到:

   $ snmpget -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0
   UCD-DEMO-MIB::ucdDemoPublicString.0 = "hi there!"
   $ snmpset -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0 s "Hello, world!"
   UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello, world!"
   $ snmpget -v 1 -c demopublic test.net-snmp.org ucdDemoPublicString.0
   UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello, world!"

请注意,在 SET 请求之后返回的值将始终与提供的值相同。这通常与后续 GET 请求返回的相同(如上所示),但不一定:

$ snmpget test.net-snmp.org  snmpSetSerialNo.0
   SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123456
   $ snmpset test.net-snmp.org  snmpSetSerialNo.0 i 123456
   SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123456
   $ snmpget test.net-snmp.org  snmpSetSerialNo.0
   SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123457

多个值

snmpget命令一样,也可以在一个请求中设置多个新值。只需在命令行上指定 (OID,type,value) 三元组的列表:

   $ snmpset test.net-snmp.org ucdDemoPublicString.0 s "Hello sky" snmpSetSerialNo.0 i 123457 
   UCD-DEMO-MIB::ucdDemoPublicString.0 = "Hello sky" 
   SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 123457

如果这些分配中的一个无效,那么请求将被拒绝而不应用 任何新值 - 无论它们在列表中出现的顺序如何。

失败的请求

如果 MIB 文件已加载,并且根据 MIB 定义提供的值无效(例如错误的类型,或超出该对象的有效范围)

   $ snmpset test.net-snmp.org  snmpSetSerialNo.0 s "as any fule kno"
   snmpSetSerialNo.0: Bad variable type
       (Type of attribute is INTEGER, not OCTET STRING)

如果 MIB 文件不可用,或者值与 MIB 定义中的语法匹配,则请求将发送到目标代理,然后目标代理可能会拒绝请求:

   $ snmpset test.net-snmp.org  snmpSetSerialNo.0 i 9999
   Error in packet.
   Reason: (badValue) The value given has the wrong type or length
   Failed object: SNMPv2-MIB::snmpSetSerialNo.0

如果您没有写入对象的权限,根据所使用的 SNMP 版本,报告的错误会有所不同:

$ snmpset -v 1  -c rocommunity test.net-snmp.org  snmpSetSerialNo.0 i 123457
   Error in packet.
   Reason: (noSuchName) There is no such variable name in this MIB.
   Failed object: SNMPv2-MIB::snmpSetSerialNo.0
   $ snmpset -v 2c -c rocommunity test.net-snmp.org  snmpSetSerialNo.0 i 123457
   Error in packet.
   Reason: noAccess
   Failed object: SNMPv2-MIB::snmpSetSerialNo.0
   <tasks>[ ] Should this return notWritable?</tasks>

 类似资料: