Python paramiko module for remote control and transmission, pythonparamiko
Reprinted please indicate the source
1 Installation
sudo pip install paramiko
2 ssh remote control
# Execute shellssh username @ ip in LINUX # enter the password to operate on the remote machine ssh username @ ip command # enter the password and then run the command on the remote machine.
After ssh is run, kill the ssh process if you want to quit.
3 paramiko implement ssh
Import paramikohostname = '10. 1.111.111 'username = 'root' password = '000000' port = 22 # The integer is not a string paramiko. util. log_to_file ('paramiko. log') ssh = paramiko. SSHClient () ssh. set_missing_host_key_policy (paramiko. autoAddPolicy () # Allow connection to host ssh that is not in the know_hosts file. connect (hostname = hostname, port = port, username = username, password = password) stdin, stdout, stderr = ssh.exe c_command ("ls") # remotely execute the shell command print (stdout. readlines () # output echo result ssh. close ()
Exec_command command, with a semicolon;Separate Two commands; multiple parameters can be input; exec_command is a single session. After the execution is complete, it will return to the default directory at logon, as shown below:
A = '~ /Videos' B ='aaa'ssh.exe c_command ('CD % s; mkdir % s' % (a, B) Then aaaunder adirectory ssh.exe c_command ('mkdir aaa') # aaa is in the default directory
If there is interaction during remote command execution, you can use stdin. write ("") to complete it.
Note:
(1) After sudo, add-STo receive the password from stdin;
(2) stdin. write ('password \ n ')\ NThe end of the command; otherwise, the server waits;
(3) flush () write buffer (flush () any buffer you're writing)
stdin, stdout, stderr = ssh.exec_command('sudo -S ls')stdin.write('password\n')stdin.flush()
You can use multiple processes or threads to execute commands in batches:
import paramikoimport threadingdef ssh_cmd(ip,port,username,passwd,cmd): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,port,username,passwd) for m in cmd: stdin, stdout, stderr = ssh.exec_command(m) print(stdout.readlines()) ssh.close()if __name__=='__main__': cmd = ['ls','ifconfig'] a=threading.Thread(target=ssh_cmd,args=(ip,port,username,passwd,cmd)) a.start() a.join()
4. Remote File Transfer
Copy scp from local server to remote server
scp local_file remote_username@remote_ip:remote_file
Specify the user name. After the command is executed, enter the user password. If the user name is not specified, enter the user name and password after the command is executed. copy the file from a distance to the local device.
scp remote_username@remote_ip:remote_file local_file
5 paramiko Remote File Transfer
Create an SFTPClient object. The object is retained with the previous SSH connection. Therefore, user authentication is not required when sftp is used to transfer files. Implement file upload:
Sftp = paramiko. SFTPClient. from_transport (ssh. get_transport () # sftp = ssh. open_sftp () # select either of the two to use sftp. put ('bbb. c', 'aaa. c ') # upload the file and rename sftp. close ()
You can upload and download files as follows:
Import paramikot = paramiko. transport (hostname, port) t. connect (username = username, password = password) sftp = paramiko. SFTPClient. from_transport (t) sftp. put ('bbb. c', 'aaa. c ') # upload the file and rename sftp. get ('m. py', 'Mm. py ') # download and rename the file t. close ()
Official documentation