@celery.task(bind=True) def scan_device_async(self, device, devicetype, manufacturer, pwdh): """ Calls the scan_device function async way. """ scan_device(device, devicetype, manufacturer, pwdh, async=self) def scan_device(device, devicetype, manufacturer, pwdh, async=None): """ Scan a single device. Device is a pickle object. TODO: Async information for each device """ password = PasswordManager.decrypt_string(device.password, pwdh) enapassword = PasswordManager.decrypt_string(device.enapassword, pwdh) derror = {} app.logger.info("Started for {}".format(device.name)) if device.method in ['ssh', 'telnet']: if async: async.update_state(state='PROGRESS', meta={'message': "Connecting", 'percentage': 5}) if device.method == "ssh": s_tunnel = 'ssh -o ConnectTimeout=25 ' child = pexpect.spawn(s_tunnel + device.username + '@' + device.ip) else: s_tunnel = 'telnet ' child = pexpect.spawn(s_tunnel + device.ip) q = child.expect(generate_pexpect_list([PROMPT_REGEX_CISCO])) if perror(device, derror, m): return derror
def mainsendcommand(): # derror={} exit_flag = 0 class myThread(threading.Thread): def __init__(self, threadID, name, q): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.q = q def run(self): print("Starting " + self.name) process_data(self.name, self.q) print("Exiting " + self.name) def process_data(threadName, q): while not exit_flag: queueLock.acquire() if not workQueue.empty(): data = q.get() queueLock.release() print( send(data)) else: queueLock.release() time.sleep(1) threadList = ["Thread-1", "Thread-2", "Thread-3"] queueLock = threading.Lock() workQueue = Queue.Queue(2000) threads = [] threadID = 1 nameList = [] for device in Device.query.all(): password = PasswordManager.decrypt_string(device.password, PasswordManager.generate_pwdh_from_password("root")) username = device.username method = 'ssh' enable = PasswordManager.decrypt_string(device.enapassword, PasswordManager.generate_pwdh_from_password("root")) nameList.append(method + "," + device.name + "," + device.ip + "," + username + "," + password + "," + enable + "," + device.devicetype.manufacturer.name) # Create new threads for tName in threadList: thread = myThread(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1 # Fill the queue queueLock.acquire() for word in nameList: print(word) workQueue.put(word) queueLock.release() # Wait for queue to empty while not workQueue.empty(): pass # Notify threads it's time to exit exit_flag = 1 # Wait for all threads to complete for t in threads: t.join() print("Exiting Main Thread")