为什么 pysnmp "get next or nextcmd" 只显示两个接口?
why pysnmp "get next or nextcmd" showing only two interfaces?
在我的虚拟盒子中有四个接口和环回接口,如下所示。 eth0, eth1, eth2,eth3, lo.
VirtualBox:~/pysnmp# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:e1:eb:b9 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:10:b8:4b brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:94:00:83 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:62:2b:9f brd ff:ff:ff:ff:ff:ff
当我尝试使用 pysnmp nextcmd 获取接口信息时,它总是只显示两个接口 eth0, lo.
使用这个例子:
from pysnmp.hlapi import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')),
ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')),
ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
lexicographicMode=False):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
break
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
输出:
root@VirtualBox:~/pysnmp# python tutorial.py
IF-MIB::ifDescr.1 = lo
IF-MIB::ifType.1 = softwareLoopback
IF-MIB::ifMtu.1 = 16436
IF-MIB::ifSpeed.1 = 10000000
IF-MIB::ifPhysAddress.1 =
IF-MIB::ifType.1 = softwareLoopback
IF-MIB::ifDescr.2 = eth0
IF-MIB::ifType.2 = ethernetCsmacd
IF-MIB::ifMtu.2 = 1500
IF-MIB::ifSpeed.2 = 100000000
IF-MIB::ifPhysAddress.2 = 00:12:79:62:f9:40
IF-MIB::ifType.2 = ethernetCsmacd
剩下的接口呢?是不是被显示了?有什么想法吗?
UdpTransportTarget(('demo.snmplabs.com', 161))
。目标 IP 地址是 demo.snmplabs.com。它正在从此 IP 地址获取接口名称。
要获取本地机器(虚拟机)中的接口,修改逻辑如下。
CommunityData('public', mpModel=0), < "Community string is what you configured in your local machine refer snmd.conf file">
UdpTransportTarget(('localhost', 161)). <Target IP address is our local host>
在我的虚拟盒子中有四个接口和环回接口,如下所示。 eth0, eth1, eth2,eth3, lo.
VirtualBox:~/pysnmp# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:e1:eb:b9 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:10:b8:4b brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:94:00:83 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:62:2b:9f brd ff:ff:ff:ff:ff:ff
当我尝试使用 pysnmp nextcmd 获取接口信息时,它总是只显示两个接口 eth0, lo.
使用这个例子:
from pysnmp.hlapi import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')),
ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')),
ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')),
ObjectType(ObjectIdentity('IF-MIB', 'ifType')),
lexicographicMode=False):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
break
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
输出:
root@VirtualBox:~/pysnmp# python tutorial.py
IF-MIB::ifDescr.1 = lo
IF-MIB::ifType.1 = softwareLoopback
IF-MIB::ifMtu.1 = 16436
IF-MIB::ifSpeed.1 = 10000000
IF-MIB::ifPhysAddress.1 =
IF-MIB::ifType.1 = softwareLoopback
IF-MIB::ifDescr.2 = eth0
IF-MIB::ifType.2 = ethernetCsmacd
IF-MIB::ifMtu.2 = 1500
IF-MIB::ifSpeed.2 = 100000000
IF-MIB::ifPhysAddress.2 = 00:12:79:62:f9:40
IF-MIB::ifType.2 = ethernetCsmacd
剩下的接口呢?是不是被显示了?有什么想法吗?
UdpTransportTarget(('demo.snmplabs.com', 161))
。目标 IP 地址是 demo.snmplabs.com。它正在从此 IP 地址获取接口名称。
要获取本地机器(虚拟机)中的接口,修改逻辑如下。
CommunityData('public', mpModel=0), < "Community string is what you configured in your local machine refer snmd.conf file">
UdpTransportTarget(('localhost', 161)). <Target IP address is our local host>