snmp discovery with
短信预约 -IT技能 免费直播动态提醒
snmp discovery with python
- #!/usr/bin/env python
- from processing import Process, Queue, Pool
- import time
- import subprocess
- from IPy import IP
- import sys
- from snmp import Snmp
-
- q = Queue()
- oq = Queue()
- #ips = IP("10.0.1.0/24")
- ips = ["10.10.10.28","10.10.10.29","10.10.10.30","10.10.10.31","10.10.10.32","10.10.10.33","10.10.10.34","10.10.10.35","10.10.10.36","10.10.10.37","10.10.10.38","10.10.10.39"]
- num_workers = 10
-
- class HostRecord(object):
- """Record for Hosts"""
- def __init__(self, ip=None, mac=None, snmp_response=None):
- self.ip = ip
- self.mac = mac
- self.snmp_response = snmp_response
- def __repr__(self):
- return "[Host Record('%s','%s','%s')]" % (self.ip,
- self.mac,
- self.snmp_response)
-
- def f(i,q,oq):
- while True:
- time.sleep(.1)
- if q.empty():
- sys.exit()
- print "Process Number: %s Exit" % i
- ip = q.get()
- print "Process Number: %s" % i
- ret = subprocess.call("ping -c 1 %s" % ip,
- shell=True,
- stdout=open('/dev/null', 'w'),
- stderr=subprocess.STDOUT)
- if ret == 0:
- print "%s: is alive" % ip
- oq.put(ip)
- else:
- print "Process Number: %s didn't find a response for %s " % (i, ip)
- pass
-
- def snmp_query(i,out):
- while True:
- time.sleep(.1)
- if out.empty():
- sys.exit()
- print "Process Number: %s" % i
- ipaddr = out.get()
- s = Snmp()
- h = HostRecord()
- h.ip = ipaddr
- h.snmp_response = s.query()
- print h
- return h
- try:
- q.putmany(ips)
-
- finally:
- for i in range(num_workers):
- p = Process(target=f, args=[i,q,oq])
- p.start()
- for i in range(num_workers):
- pp = Process(target=snmp_query, args=[i,oq])
- pp.start()
-
- print "main process joins on queue"
- p.join()
- #while not oq.empty():
- # print "Validated", oq.get()
-
-
- print "Main Program finished"
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341