ssh batch execute commands - paramiko

---Restore content begins---

1  # python3.5 + paramiko 
2  # pip is a package management tool for python, execute the following command in the shell to install the paramoko module 
3  # pip install paramiko 
4  #
 5  
6  import paramiko
 7  
8  def ssh_bat_cmd(ip, port,username,password, command):
 9      ssh = paramiko.SSHClient ()
 10      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 11      # ssh.connect("172.16.2.10", 22, "root", "123123") 
12      ssh.connect(ip , port, username, password)    #Note that the IP username and password here are all strings 
13      #stdin, stdout, stderr = ssh.exec_command(command)
14     stdin, stdout, stderr = ssh.exec_command(command) #注意这里的command 是字符串
15 
16     stdout_info = stdout.readlines()
17     err_info = stderr.readlines()
18     if err_info:
19         print("{} is failed: {}".format(ip,err_info))
20     else:
21         print("{} is successful: {}".format(ip, stdout_info))
22     ssh.close()
23 
24 IP_dic  = {
25     #    "IP":[port,"username","password"],
26     "1.1.1.2":[22,"username","password"],
27     "1.1.1.3":[22,"username","password"],
28     "1.1.1.4":[22,"username","password"],
29 }
30 
31 for ip in IP_dic:
32     ssh_bat_cmd(ip, IP_dic[ip][0], IP_dic[ip][1], IP_dic[ip][2], """  df -Th   """)

 

 

---End of recovery content---

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344448&siteId=291194637