I think you'd better to use multiple threads. Here is an example:
Better not to use 'exec_command' command.
SSH client will be closed if this command is executed.
And you have to reconnect to the server if you want to execute other commands. So:
'chan = ssh_client.invoke_shell()' is a good choice.
# _*_ coding:utf-8 _*_ import time import os import paramiko import threading import datetime import traceback import re current_path = os.getcwd() # customize the following path # the content of host list file should be: # officename swname swip username password # eg: marvin huawei 10.0.0.7 jack 123456 host_list_path = current_path + '/sw.txt' # puts your commands to this file one by one line command_list_path = current_path + '/command.txt' # Don't add backslash at the end of path. log_path = '/home/seven/log' # get current time now = datetime.datetime.now() # get thread lock thread = threading.Lock() # thread list threads = [] # success hosts list success_hosts = [] # failed hosts list fail_hosts = [] #Use for loop to telnet into each routers and execute commands class Bakconf(threading.Thread): def __init__(self, swname, host, username, password, port, day_dir, screen_command): threading.Thread.__init__(self) self.swname = swname self.host = host self.username = username self.password = password self.port = port self.log_dir = day_dir self.screen_command = screen_command def run(self): try: paramiko.util.log_to_file('ssh.log') ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) print("Connecting to server %s" % self.host) ssh_client.connect(self.host, self.port, self.username, self.password, allow_agent = False, look_for_keys = False, timeout=10) print("Successfully connected to server %s." % self.host) chan = ssh_client.invoke_shell() time.sleep(2) if chan.send_ready(): chan.send(self.screen_command+"\n") else: raise("Channel not ready!") sleep_time = 10 # execute commands in commands file. for command in open(command_list_path, 'r').read().splitlines(): chan.send(command+"\n") print("%s is running. Sleep for %d seconds!" % (self.host, sleep_time)) time.sleep(sleep_time) # write output to log file. filename = self.log_dir + "/%s:%i-%.2i-%.2i-%.2i:%.2i:%.2i.log" % (self.swname, now.year, now.month, now.day, now.hour, now.minute, now.second) with open(filename,"ab+") as fp: resp = chan.recv(9999999) fp.write(resp) print("Host %s was exported Successfully!" % self.host) chan.send('quit\n') ssh_client.close() success_hosts.append(self.host) except: fail_hosts.append(self.host) print("Can't connect to %s" % self.host) print("port=%s, username=%s, password=%s" % (self.port, self.username, self.password)) traceback.print_exc() print("Thread exit!\n") return def main(): for line in open(host_list_path, 'r').read().splitlines(): if line: try: officename, swname, swip, username, password, screen_command = re.split(',\s?', line) except: print("\nThe style of host list file is incorrect.\n")292.82 return office_dir = log_path + "/" + officename if not os.path.exists(office_dir): os.makedirs(office_dir) day_dir = office_dir + "/%i-%.2i-%i" % (now.year, now.month, now.day) if not os.path.exists(day_dir): os.mkdir(day_dir) bakconf_thread = Bakconf(swname, swip, username, password, 22, day_dir, screen_command) bakconf_thread.start() threads.append(bakconf_thread) for t in threads: t.join() print("Finish!\nSucceeded hosts: %s\nFailed hosts: %s" % (success_hosts, fail_hosts)) if __name__=="__main__": main()
Better not to use 'exec_command' command.
SSH client will be closed if this command is executed.
And you have to reconnect to the server if you want to execute other commands. So:
'chan = ssh_client.invoke_shell()' is a good choice.