查看服务端配置:https://www.cnblogs.com/dpf-10/p/9175409.html
查看内容示例:
D:python>snmpwalk -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.2021.5000 UCD-SNMP-MIB::ucdavis.5000.1.0 = INTEGER: 1 UCD-SNMP-MIB::ucdavis.5000.2.1.2.10.103.101.116.119.105.110.105.110.102.111 = STRING: "C:/test/test.ba UCD-SNMP-MIB::ucdavis.5000.2.1.3.10.103.101.116.119.105.110.105.110.102.111 = "" UCD-SNMP-MIB::ucdavis.5000.2.1.4.10.103.101.116.119.105.110.105.110.102.111 = "" UCD-SNMP-MIB::ucdavis.5000.2.1.5.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 5 UCD-SNMP-MIB::ucdavis.5000.2.1.6.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 1 UCD-SNMP-MIB::ucdavis.5000.2.1.7.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 1 UCD-SNMP-MIB::ucdavis.5000.2.1.20.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 4 UCD-SNMP-MIB::ucdavis.5000.2.1.21.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 1 UCD-SNMP-MIB::ucdavis.5000.3.1.1.10.103.101.116.119.105.110.105.110.102.111 = STRING: "test123456" UCD-SNMP-MIB::ucdavis.5000.3.1.2.10.103.101.116.119.105.110.105.110.102.111 = STRING: "test123456" UCD-SNMP-MIB::ucdavis.5000.3.1.3.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 1 UCD-SNMP-MIB::ucdavis.5000.3.1.4.10.103.101.116.119.105.110.105.110.102.111 = INTEGER: 0 UCD-SNMP-MIB::ucdavis.5000.4.1.2.10.103.101.116.119.105.110.105.110.102.111.1 = STRING: "test123456"
根据回显内容,修改oid,在原OID后添加上方标红ID,重新获取内容:
D:python>snmpwalk -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.2021.5000.3.1.1.10.103.101.116.119.105.110.105.110.102.111 UCD-SNMP-MIB::ucdavis.5000.3.1.1.10.103.101.116.119.105.110.105.110.102.111 = STRING: "test123456" D:python>
直接获取到所需内容行,接下来就是解析改行内容,再此之前,发现如果获取内容包含中文时,回显内容为“HEX-STRING:”,示例:
D:python>snmpwalk -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.2021.5000.3.1.1.10.103.101.116.119.105.110.105.110.102.111 UCD-SNMP-MIB::ucdavis.5000.3.1.1.10.103.101.116.119.105.110.105.110.102.111 = Hex-STRING: E6 B5 8B E8 AF 95 31 32 33 34 35 36 D:python>
所以,我们需要对回显结果进行判断后解析,python脚本为:
# coding=utf-8 import re import os,sys import time import platform if 'Windows' == platform.system(): hosts = ['127.0.0.1'] else: # 在虚拟机运行时则查看本地 hosts = ['127.0.0.1'] def snmpWalk(host, oid): result = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read() return result #snmpwalk -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.2021.5000 #处理回显结果system def getSystem(host): system = snmpWalk(host, '.1.3.6.1.4.1.2021.5000.3.1.2.10.103.101.116.119.105.110.105.110.102.111') if system.find('Hex-STRING:') > 0: system = "".join(system.split('Hex-STRING:')[1:]) system = "".join(system.split(' ')) system = "".join(system.split(' ')) system = system.decode("hex") else: system = "".join(system.split('STRING: "')[1:]) system = system[0:len(system)-2] return system def main(): for host in hosts: print('=' * 10 + host + '=' * 10) print(u"测试信息") system = getSystem(host) print system my_file = 'C:/test/snmpTestinfo' print ("信息存储到文件: %s"%(my_file)).decode('utf-8').encode('gbk') if os.path.exists(my_file): os.remove(my_file) f = open(my_file, 'a+') f.write(str(system)) f.close() if __name__ == '__main__': main()
查看存储文件内容,支持各类文件。