Paramiko module,

Source: Internet
Author: User

Paramiko module,

The paramiko module of python is used to connect to a remote server and perform related operations based on ssh.

 

SSHClient

Connect to the remote server and execute the following basic commands:

User name and password-based connection:

1 import paramiko 2 3 # create an SSH object 4 ssh = paramiko. SSHClient () 5 # Allow connection to host 6 ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () 7 # connect to Server 8 ssh. connect (hostname = '0. 0.0.0 ', port = 22, username = 'root', password = '000000') 9 10 # Execute Command 11 stdin, stdout, stderr = ssh.exe c_command ('df ') 12 # obtain command Result 13 # The standard output is output only when it is correct. If it is incorrect, No 14 res, err = stdout. read (), stderr. read () 15 result = res if res else err16 print (result. decode () 17 # Close connection 18 ssh. close ()

If you encounter such a problem

Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())This function can be effectively solved.

Explanation:

In linux, directly log on to the remote through ssh, and the following problems may occur:

After entering yes:

Enter the password to log on.

In linux, if there is a file/root/. ssh/known_hosts

There is an rsa information related to 10.0.0.41. If this file contains secure signature authentication, you can directly enter a password to connect to the file. Otherwise, you need to determine whether to add known_hosts.

Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())It solves this problem and automatically adds Authentication

In this case, the "top" command cannot be executed. Instead, the "top" command can only be executed once.

 

SFtpClient

Ssh remote file transfer command:

# R. Even if the directory is used, p transfers the File Permission.
Scp-rp-P52113 file name root@10.0.0.41:/tmp/
View the file content. You can use more *** to view the directory content ****

Used to connect to the remote server and perform upload and download

Upload and download based on user name and password

Import paramikotransport = paramiko. transport ('2017. 168.5.9 ', 22) transport. connect (username = 'root', password = '000000') sftp = paramiko. SFTPClient. from_transport (transport) # Set location. upload py to the server/tmp/test. pysftp. put ('note', '/tmp/test_from_win') # download remove_path to the local local_pathsftp.get ('/root/test.txt', 'fromlinux.txt ') transport. close ()

The put () method is to upload files, and the get () method is to download files.

The above two methods both use the plaintext user name and password, which is very insecure. So you can use the key to connect

Ssh key ----- RSA asymmetric key verification

Public key

Private key)

10.0.0.31 ---------- 10.0.0.41.

Private Key

In linux:

A. Generate a public key private key pair in linux: # ssh-keygen (stored in/root/. ssh/id_rsa by default)

The private key is in id_rsa, and the public key is in id_rsa.pub.

B. Add the Local Public Key to the authorized_keys of the remote machine to achieve SSH password-less login. Ssh-copy-id "-p22 root@192.168.5.9"

C. Remote Password-less login via ssh: ssh root@192.168.5.9-p22

In windows:

Import paramikoprivate_key = paramiko.RSAKey.from_private_key_file('id_rsa9.txt ') # create an SSH object ssh = paramiko. SSHClient () # Allow connection to host ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () # connect to the server ssh. connect (hostname = '2017. 168.5.12 ', port = 22, username = 'xiaoming', pkey = private_key) # execute the command stdin, stdout, stderr = ssh.exe c_command ('df ') # obtain the command result = stdout. read () print (result. decode () # Close the ssh connection. close ()

 

Use the rz/sz command to upload the Public Key generated in linux to windows: sz ~ /. Ssh/id_rsa
Run the following command to install rz/sz: yum install lrzsz.
Then, put the obtained id_rsa in the project to use it.

 

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.