Python Paramiko via key file login ssh and heard sftp upload file __python

Source: Internet
Author: User
Tags stdin

Paramiko is not logged through the key file, which is a good solution, direct connect enter username and password on OK

#-*-Coding:utf-8-*-
import Paramiko
paramiko.util.log_to_file (' Paramiko.log ') #记录日志文件
ssh = Paramiko. Sshclient ()
ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
ssh.connect (' 127.0.0.1 ', username = ' root ', password= ' Hu Jintao ')
cmd = ' CD '    #进入用户目录home
Stdin,stdout,stderr = Ssh.exec_command (cmd)
print stdout.readlines () 

cmd = ' ls >test '  #管道, LS named output to file test inside
stdin,stdout,stderr = Ssh.exec_command (cmd)
print stdout.readlines ()

cmd = ' cat Test '  # Displays the contents of test, that is, the result of LS named
stdin,stdout,stderr = Ssh.exec_command (cmd)
#print stdout.readlines ()   character display for #结果 Chinese characters

Can be achieved by the above method.

When a key file is required, the Paramiko needs to be logged in the following way:

#-*-Coding:utf-8-*-
import Paramiko

pkey= ' e:/wamp/www/tools/id_rsa '  #本地密钥文件路径 [on this file server ~/.ssh/id_ RSA can be downloaded to local]
Key=paramiko. Rsakey.from_private_key_file (pkey,password= ' Hu Jintao ') #有解密密码时,
paramiko.util.log_to_file (' Paramiko.log ')
ssh = Paramiko. Sshclient ()
ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #通过公共方式进行认证 (does not need to exist in the known_hosts file)
#ssh. Load_system_host_keys () #如通过known_hosts Authentication You can use this, If the known_hosts file is undefined, you also need to define known_hosts
ssh.connect (' 127.0.0.1 ', username = ' root ', password= ' Hu Jintao ', Pkey=key) #这里要 Pkey passwordkey key file
stdin,stdout,stderr=ssh.exec_command (' hostname ')
print stdout.read ()
stdin, Stdout,stderr=ssh.exec_command (' ls ')
print stdout.read ()

File transfer via SFTP:

#-*-Coding:utf-8-*-
import paramiko
import os

pkey= ' E:/wamp/www/tools/id_rsa ' Key=paramiko
. Rsakey.from_private_key_file (pkey,password= ' Hu Jintao ')
paramiko.util.log_to_file (' paramiko.log ')

ssh = Paramiko. Sshclient ()
ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
ssh.connect (' 127.0.0.1 ', username = ' root ', password= ' Hu Jintao ', pkey=key)
t = ssh.get_ Transport ()
Sftp=paramiko. Sftpclient.from_transport (t)
d = sftp.put ("Mm.txt", "/home/www/mm.txt") 
print D
t.close ()

File downloads by sftp:

#-*-Coding:utf-8-*-
import paramiko
import os

pkey= ' E:/wamp/www/tools/id_rsa ' Key=paramiko
. Rsakey.from_private_key_file (pkey,password= ' Hu Jintao ')
paramiko.util.log_to_file (' paramiko.log ')

ssh = Paramiko. Sshclient ()
ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
ssh.connect (' 127.0.0.1 ', username = ' root ', password= ' Hu Jintao ', pkey=key)
t = ssh.get_ Transport ()
Sftp=paramiko. Sftpclient.from_transport (t)
d = sftp.get ("/home/www/mm.txt", "Mm.txt") 
print D
t.close ()

Reference Documentation:
http://5ydycm.blog.51cto.com/115934/340854/[Paramiko ssh sftp] * * * *
http://blog.chinaunix.net/uid-25979788-id-2203623.html [use Python to login Linux via SSH and operate without keys]
Http://hi.baidu.com/397090770/item/c79c7c960afad3dc1b49dfae (Putty generates a "private" key in a way that can be studied)
Http://www.cnblogs.com/gannan/archive/2012/02/06/2339883.html (Installation and use of Paramiko)
http://www.pyshell.com/index.php/archives/329 (Paramiko Module Series II: Access to the remote server with key without password)
[One: Paramiko Module series: Analog SSH login remote server]
[bis: Paramiko Module Series II: Access to remote server with key without password]
[Third: Paramiko Module series of three: Batch upload file] http://www.pyshell.com/index.php/archives/332
[The Four: Paramiko Module series of three: bulk download files to local] http://www.pyshell.com/index.php/archives/337

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.