1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| from pysnmp.hlapi import *
engine = SnmpEngine()
communityData = CommunityData('public', mpModel=1)
userData = UsmUserData( userName='admin', authKey='Admin@h3c', privKey='Admin@h3c', authProtocol=usmHMACMD5AuthProtocol, privProtocol=usmAesCfb128Protocol, )
target = UdpTransportTarget(('192.168.56.20',161))
context = ContextData()
def getSysName(target): sysname = ObjectIdentity("1.3.6.1.2.1.1.5.0") sysname1 = ObjectIdentity('SNMPv2-MIB','sysName',0) obj1 = ObjectType(sysname) g = getCmd(engine, communityData, target, context, obj1) _, _, _, result = next(g) for i in result: print(i)
def getIfaceList(target): """ 这个函数是查询接口列表,和上面查询 sysName 的区别是使用了 nextCmd 来获取一个 MIB 子树的全部内容 主要是 `lexicographicMode=False` 参数,默认为 `True`,会一直查询到 MIB 树结束。 """ ifaceListOid = ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.2')) g = nextCmd(engine,userData,target,context,ifaceListOid,lexicographicMode=False) try: while True: errorIndication, errorStatus, errorIndex, varBinds = next(g) for iface in varBinds: print(iface) except StopIteration: print('Get interface list done.')
getSysName(target) print('============================') getIfaceList(target)
|