Python paramiko module implements remote control and transmission. For example, pythonparamiko
This article describes how to implement remote control and transmission in the paramiko module of python:
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, separated by semicolons (;) indicates that two commands are executed successively. Multiple parameters can be passed in. exec_command is a single session. After the execution is complete, it will return to the default logon directory, 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) Add-S after sudo to receive the password from stdin;
(2) stdin. write ('password \ n') end with \ n as 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. Implementation 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
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.