同样使有snmp第三方包,来实现,获取设备信息
from pysnmp.hlapi import *
# 方案二 试验可行
iterator = getCmd(
SnmpEngine(),
CommunityData(communityIndex='public', communityName='snuser'),
UdpTransportTarget(('192.168.10.10', 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB','sysName',0)) # sysName 0必须跟上,否则报错。之前有提到
)
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(), varBinds[int(errorIndex)-1] if errorIndex else '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))