python snmp 其中比较常用的是netsnmp
和pysnmp
两个库,
先说netsnmp。python的netsnmp,其实是来自于net-snmp包。系统需要先安装net-snmp 我是64位系统的windows 7 安装32位的net-snmp也是可以使用的。可以选择..exe安装 ,也可以使用源码安装。在源码文件找到python 文件夹,进入后使用python setup.py install命令安装即可。
安装成功后再cmd下使用snmpwalk可以进行snmp的测试。
snmpwalk -v 1 -c public 10.10.10.223 | more
snmpwalk -v 2c -c public 10.10.10.223 | more 1用snmp版本1 2c为snmp版本2
使用pip工具安装snmp-cmds模块 pip3 install snmp-cmds 安装后python既可以通过cmd下的snmpwalk进行snmp操作了
from snmp_cmds import snmpwalk
#返回结果是列表
res = snmpwalk(ipaddress='192.168.59.251',oid='IF-MIB:ifDescr',community='qiji123')
for line in res:
#第一个元素为oid,第二个元素为接口名字
print(line[0],' ',line[1])
对以上代码进行说明,192.168.59.251为想要获得信息的客户端ip。oid 用字母的形式 不用数字的形式,可以用一下命令进行查找,我用了.1.3.6.**** 经常出现错误。
C:\>snmpwalk -v 1 -c public 10.10.10.223 | more
SNMPv2-MIB::sysDescr.0 = STRING: Hardware: x86 Family 6 Model 47 Stepping 2 AT/A
T COMPATIBLE - Software: Windows 2000 Version 5.1 (Build 2600 Multiprocessor Fre
e)
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.311.1.1.3.1.1
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (25275350) 2 days, 22:12:33.50
SNMPv2-MIB::sysContact.0 = STRING:
SNMPv2-MIB::sysName.0 = STRING: S11111
SNMPv2-MIB::sysLocation.0 = STRING:
SNMPv2-MIB::sysServices.0 = INTEGER: 76
IF-MIB::ifNumber.0 = INTEGER: 2
IF-MIB::ifIndex.1 = INTEGER: 1
IF-MIB::ifIndex.2 = INTEGER: 2
IF-MIB::ifDescr.1 = STRING: MS TCP Loopback interface
IF-MIB::ifDescr.2 = STRING: VMware Accelerated AMD PCNet Adapter - 数据包计划程
序微型端
IF-MIB::ifType.1 = INTEGER: softwareLoopback(24)
IF-MIB::ifType.2 = INTEGER: ethernetCsmacd(6)
IF-MIB::ifMtu.1 = INTEGER: 1520
IF-MIB::ifMtu.2 = INTEGER: 1500
IF-MIB::ifSpeed.1 = Gauge32: 10000000
IF-MIB::ifSpeed.2 = Gauge32: 1000000000
IF-MIB::ifPhysAddress.1 = STRING:
IF-MIB::ifPhysAddress.2 = STRING: 0:c:29:b2:f3:5e
IF-MIB::ifAdminStatus.1 = INTEGER: up(1)
IF-MIB::ifAdminStatus.2 = INTEGER: up(1)
IF-MIB::ifOperStatus.1 = INTEGER: up(1)
IF-MIB::ifOperStatus.2 = INTEGER: up(1)
IF-MIB::ifLastChange.1 = Timeticks: (0) 0:00:00.00
IF-MIB::ifLastChange.2 = Timeticks: (0) 0:00:00.00
IF-MIB::ifInOctets.1 = Counter32: 1196
IF-MIB::ifInOctets.2 = Counter32: 129709634
IF-MIB::ifInUcastPkts.1 = Counter32: 12
IF-MIB::ifInUcastPkts.2 = Counter32: 519454
IF-MIB::ifInNUcastPkts.1 = Counter32: 0
IF-MIB::ifInNUcastPkts.2 = Counter32: 741267
IF-MIB::ifInDiscards.1 = Counter32: 0