1). Linux system first to open SSH Service: Service SSH status
If not installed, then: Apt-get install Openssh-server
Service SSH Restart
2). Pip Install Paramiko
Example 1:
Import Paramiko
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Ssh.connect (' 192.168.80.139 ', username = ' Allen ', password = ' Allen ', timeout = 300)
cmd = ' CD '
stdin, stdout, stderr = Ssh.exec_command (cmd)
cmd = ' ls python '
stdin, stdout, stderr = Ssh.exec_command (cmd)
Print Stdout.read ()
#for std in Stdout.readlines ():
# Print Std
Ssh.close ()
If you run this script after the "Multibackend cannot is initialized with no backends. If you is seeing this error when trying to use Default_backend () Try uninstalling and reinstalling Crypto Graphy. "This error, then:
pip uninstall paramikopip install paramiko==1.17
Reference: http://stackoverflow.com/questions/37135521/ Paramiko-transport-throws-runtime-valueerror-while-connecting-to-remote-server-u
Script Twoexecute the appropriate command on the remote server
Import Sys
Import Paramiko
hostname = Sys.argv[1]
Command = "". Join (sys.argv[2:])
Port=22
Username= "Allen"
Password= "Allen"
If __name__== "__main__":
S=paramiko. Sshclient ()
S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
S.connect (Hostname,port,username,password)
Stdin,stdout,sterr=s.exec_command (command)
Print Stdout.read ()
S.close ()
Script Three: Manage multiple servers: Bulk query The disk usage of the corresponding server in the IP list
Import Paramiko
Port = 22
Username = "Allen"
File=open ("Ip.list")
For line in file:
hostname = STR (line.split ("T") [1])
Password = str (line.split ("T") [4]). Strip ()
Print "##########################", hostname, "########################"
s = Paramiko. Sshclient ()
S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
S.connect (hostname, port, username, password)
Stdin,stdout,sterr = S.exec_command ("df-th")
Print Stdout.read ()
S.close ()
File.close ()
Ip.list content:
DX 192.168.0.1 Root Loveyou
script four: Similar to script two, execute the appropriate command on all remote servers
Import Paramiko
Import Sys
Port=22
Username= "Root"
Command = "". Join (sys.argv[1:])
File=open ("Ip.list")
For line in file:
Hostname=str (Line.split ("T") [1])
Password=str (Line.split ("T") [4]). Strip ()
Print "##################", hostname, "######################"
S=paramiko. Sshclient ()
S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
S.connect (Hostname,port,username,password)
Stdin,stdout,sterr=s.exec_command (command)
Print Stdout.read ()
S.close ()
File.close ()
import paramiko
Import sys, OS
port = 22
Username = "root"
Key_file = "~/.ssh/authorized_keys"
Know_host = "/home/larry/.ssh/known_hosts"
Command = "". Join (Sys.argv[1:]) # # # #获取命令行参数
File = open ("Ip.list")
for line in file:
hostn ame = str (Line.split ("") [1]) # # # #截取ip字段
print "#####################################", Hostname, "###############################################"
s = Paramiko. Sshclient ()
s.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
S.load_system_host_keys (know_host)
s.connect ( Hostname, port, username, key_file)
stdin, stdout, sterr = S.exec_command (command)
& nbsp; print Stdout.read (). Strip ()
s.close ()
File.close ()
Python Paramiko module ssh automatically login to Linux system for operation