Multi-device Batch SSH command, is currently serial, later will join multithreading to achieve parallel, directly on the source code
Note You cannot perform dynamic commands such as top
#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' babyshen ' __version__ = ' 1.0.0 ' Import Paramiko class SSh (object ): Def __init__ (self,port,username,password): Self.port = Port Self.username = Username SELF.PASSW Ord = password def ssh_con (self,host,ip,cmd): SSH = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (Ip,self.port,self.username,self.password) stdin, stdout, stderr = Ssh.exec_comm and (cmd) Res,err = Stdout.read (), stderr.read () result = Res If res else err print (' \033[31;1m%s%s\03 3[0m '% (HOST,IP)) print (' \033[32;1m%s\033[0m '%result.decode ()) Ssh.close () if __name__ = = ' __main__ ': cl ient = {' host1 ': ' 1.1.1.1 ', ' host2 ': ' 2.2.2.2 ', ' host3 ': ' 3.3.3.3 ', ' Host4 ': ' 4.4.4.4 ',} # Host list, hostname:ip port = 22 # port number Usernam e = ' root ' # User name password = ' 123456789 ' # password cmd = "IP route show Match 1.1.1.1 | head-1 "ssh = SSH (port,username,password) for i in Client:host,ip = I,client[i] Try:ssh . Ssh_con (Host,ip,cmd) except Windowserror:print (' \033[31;1m%s%s%s \033[0m\n '% (host,ip, ' connection attempt failed ')) Except Exception as E:print (e)