Python Learning Note-day11--paramiko

Source: Internet
Author: User
Tags stdin

Paramiko is a module in Python that follows the SSH2 protocol and supports remote service connections in an encrypted and authenticated manner

Here's how to use it:


Link Server with account password

Import paramiko# Create object ssh = Paramiko. Sshclient () #设置访问策略, deny all unknown servers by default, use Autoaddpolicy () to add server to L{hostkeys} objects, allow links to hosts that are not in the Know_host file, ssh.set_ Missing_host_key_policy (Paramiko. Autoaddpolicy ()) #链接服务器ssh. Connect (hostname= "127.0.0.1", port=22,username= "would", password= "123123") #远程执行命令, The execution results are returned to stdin.stdout.stderr three variables stdin,stdout,stderr = Ssh.exec_command ("ls/") # reading the variable value and assigning it to the variable Resultresult = Stdin.read ( ) #关闭链接ssh. Close ()


Using a key to link a server

Import paramiko# Specifies the key file Privatekey = Paramiko. Rsakey._from_private_key_file ("/home/will/id_rsa") #创建对象ssh = Paramiko. Sshclient () #设置访问策略, deny all unknown servers by default, use Autoaddpolicy () to add server to L{hostkeys} objects, allow links to hosts that are not in the Know_host file, ssh.set_ Missing_host_key_policy (Paramiko. Autoaddpolicy ()) #链接服务器ssh. Connect (hostname= "127.0.0.1", port=22,username= "would", Pkey=privatekey) #远程执行命令, Execution results are returned to stdin.stdout.stderr three variables stdin,stdout,stderr = Ssh.exec_command ("ls/") # reads the variable value and assigns a value to the variable Resultprint stdin.read () # Close link ssh.close ()


The two methods are similar, the difference is to use the key to link the server when you need to specify a key file, the link when using the key file executed.


Looking at the Sshclient class, you can see that the class inside encapsulates is actually the Transport class

In that case, we can modify the code above.

Link Server with account password

Import Paramikohostinfo = ("127.0.0.1",) Trans =paramiko. Transport (Hostinfo) trans.connect (username= "would", password= "123123") ssh = Paramiko. Sshclient () Ssh._transport = Transstdin,stdout,stderr = Ssh.exec_command ("ls/") print Stdin.read () trans.close ()

Using a key to link a server

Import Paramikoprivatekey = Paramiko. Rsakey._from_private_key_file ("/home/will/id_rsa") Hostinfo = ("127.0.0.1", "a") trans =paramiko. Transport (Hostinfo) trans.connect (username= "would", pkey=privatekey) ssh = Paramiko. Sshclient () Ssh._transport = Transstdin,stdout,stderr = Ssh.exec_command ("ls/") print Stdin.read () trans.close ()


SFTP in the Paramiko

Upload and download based on user name password

Import Paramiko transport = Paramiko. Transport (' 127.0.0.1 ', ()) Transport.connect (username= ' 'll ', password= ' 1 ') sftp = Paramiko. Sftpclient.from_transport (transport) # upload/tmp/localfile.py file to server/tmp/remotefile.pysftp.put ('/tmp/localfile.py ', ' /tmp/remotefile.py ') # download remotefile.py to local RemoteFile.pysftp.get (' remotefile.py ', ' remotefile.py ') transport.close ( )

Upload and download based on public key keys

Import Paramiko Private_key = Paramiko. Rsakey.from_private_key_file ('/home/will/id_rsa ') transport = Paramiko. Transport (' 127.0.0.1 ', ()) Transport.connect (username= ' Wupeiqi ', pkey=private_key) sftp = Paramiko. Sftpclient.from_transport (transport) # upload/tmp/localfile.py file to server/tmp/remotefile.pyftp.put ('/tmp/localfile.py ', '/ tmp/remotefile.py ') # download remotefile.py to local RemoteFile.pysftp.get (' remotefile.py ', ' remotefile.py ') transport.close ()




Reference

Http://www.cnblogs.com/wupeiqi/articles/5095821.html

This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1735495

Python Learning Note-day11--paramiko

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.