#!/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 = Ten class Hostrecord (objec
T): "" "" "" "" "" "" "" "" "", "" "," "" "__init__ (Self, ip=none, Mac=none, Snmp_response=none): Self.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 to%s '% (i, IP) pass def snmp_q
Uery (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:forI in range (num_workers): p = Process (target=f, Args=[i,q,oq]) P.start () to I in range (num _workers): pp = Process (target=snmp_query, Args=[i,oq]) Pp.start () print "Main proces s joins on \ P.join () #while not Oq.empty (): # print "Validated", Oq.get () print "Ma In the program finished "
This article comes from "Itnihao Technology blog" blog, please be sure to keep this source http://itnihao.blog.51cto.com/1741976/1161740