Examples of remote control and transmission of Paramiko modules in Python

Source: Internet
Author: User
This article mainly introduced the Python Paramiko module realizes the remote control and the transmission example, very has the practical value, the need friend can refer to the next

This article describes the Python Paramiko module for remote control and transmission examples, to share with you, specifically as follows:

1 installation


sudo pip install Paramiko

2 SSH for remote control


#LINUX下执行shellssh username@ip #输入密码后就可以对远程机器进行操作ssh username@ip command #输入密码后远程机器就执行command

After SSH runs, you want to quit, you can kill the SSH process.

3 Paramiko Implementing SSH


Import paramikohostname = ' 10.1.111.111 ' username = ' root ' password = ' 111111 ' port = #整数不是字符串paramiko. Util.log_to_file ( ' Paramiko.log ') ssh = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #允许连接不在know_hosts文件中的主机ssh. Connect (hostname=hostname,port=port,username=username,password= Password) stdin, stdout, stderr = Ssh.exec_command ("ls") #远程执行shell命令print (Stdout.readlines ()) #输出回显结果ssh. Close ()

Exec_command command, separated by a semicolon, to execute two commands successively, multiple parameters can be passed in, Exec_command for a single session, and then returned to the default directory at logon when the execution is complete, as follows:


A= ' ~/videos ' b= ' aaa ' Ssh.exec_command (' cd%s;mkdir%s '% (A, b)) #aaa在a目录下ssh. Exec_command (' mkdir aaa ') #aaa在缺省目录下

If there is interaction at the remote execution of the command, you can do so with Stdin.write ("").

Attention:

(1) After sudo to add-s, indicating that the password received from the stdin;

(2) Stdin.write (' password\n ') last to add \ n as the end of the command, otherwise the server has been waiting;

(3) flush () write buffer (flush () Any buffer you ' re writing to)


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 bulk:


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 Transferring files remotely

SCP replicated from the local server to the remote server


SCP Local_file Remote_username@remote_ip:remote_file

The user name is specified, the user password is required after the command executes, and if the user name is not specified, a user name and password are required after the command executes; copying files from a distance to a local


SCP Remote_username@remote_ip:remote_file Local_file

5 Paramiko implement remote transfer of files

Creates a new Sftpclient object that re-uses the previous SSH connection, so when we use SFTP to transfer files, we do not need to authenticate the user again. Implementation file

Upload:


SFTP = Paramiko. Sftpclient.from_transport (Ssh.get_transport ()) #sftp = Ssh.open_sftp () #两者选其一即可sftp. Put (' bbb.c ', ' aaa.c ') # File Upload and rename sftp.close ()

or upload and download the following files:


Import Paramikot=paramiko. Transport ((Hostname,port)) T.connect (username=username,password=password) sftp = Paramiko. Sftpclient.from_transport (t) sftp.put (' bbb.c ', ' aaa.c ') #文件上传并重命名sftp. Get (' m.py ', ' mm.py ') #文件下载并重命名t. Close ()

Official documents

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.