【发布时间】:2021-12-10 09:22:59
【问题描述】:
我开始处理snmp。我想从我的网络中的设备获取一些信息,例如 ip 地址、mac 地址、名称等。我想做网络发现的事情。 我有下面的代码。我不想为这些信息使用 oid 数字。有没有办法在没有oid的情况下获取信息? 另外,输出是我的代码错误。字符显示不正确
代码:
from pysnmp.hlapi import *
import datetime
class SNMP_QUERY():
def __init__(self):
pass
def __del__(self):
pass
def snmp_query(host, community, oid):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(community),
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lookupMib=False))
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1] or '?'
)
)
else:
for name, val in varBinds:
return (str(val))
def main():
# Network Device
host = '127.0.0.1'
community = 'public'
#overviewModelName = '.1.3.6.1.2.1.1.1.0'
name = '1.3.6.1.2.1.1.5.0'
macAddres = '.1.3.6.1.2.1.2.2.1.6.12'
io_address = '.1.3.6.1.2.1.4.21.1.1.192.168.1.27'
result = {}
# result['Model Name'] = snmp_query(host, community, overviewModelName)
result['Name'] = snmp_query(host, community, name)
result['Mac Adress'] = snmp_query(host, community, macAddres)
result['IP'] = snmp_query(host, community, io_address)
print(result)
if __name__ == '__main__':
main()
输出::
{'Name': 'DESKTOP-ANIHIT4', 'Mac Adress': 'ÜA©Hð&', 'IP': 'À¨\x01\x1b'}
【问题讨论】:
-
您的输出似乎正确。 IP 和 MAC 地址不是字符串,而是一系列字节。检查this answer,了解如何将它们转换为人类可读的文本。
标签: python snmp pysnmp snmp-trap