前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >snmp discovery with

snmp discovery with

作者头像
py3study
发布2020-01-08 17:48:41
1.2K0
发布2020-01-08 17:48:41
举报
文章被收录于专栏:python3python3

 snmp discovery with python

代码语言:javascript
复制
#!/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" 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档