在我的虚拟盒子中有四个接口,环回接口如下。 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.xxxx.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
剩下的接口怎么样?是不是没有显示出来?有什么想法吗?
要获取本地机器(虚拟盒子)中的接口,请修改如下逻辑。
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>
UdpTransportTarget(('demo.xxxx.com', 161))
。目标IP地址是demo.xxxx.com。它正在从此 IP 地址获取接口名称。