Python's method of implementing remote copy files based on SFTP and RSA keys _python

Source: Internet
Author: User
Tags in python

This example describes Python's method of implementing remote copy files based on SFTP and RSA keys. Share to everyone for your reference, specific as follows:

If you use the RSA secret key password-free login between the two servers, you can first find the corresponding directory of the RSA secret key (such as found/-name Id_rsa or locate Id_rsa).

The SCP functionality can then be implemented through the Paramiko module in Python:

def scp_by_key (Host_ip, Host_port, Remote_path, Local_path, username, pkey_path):
  try:
    Key=paramiko. Rsakey.from_private_key_file (pkey_path)
    t = Paramiko. Transport ((HOST_IP, Host_port))
    t.connect (Username=username, pkey=key)
    sftp = Paramiko. Sftpclient.from_transport (t)
    src = remote_path
    des = Local_path
    sftp.get (src,des)
    t.close ()
  Except Exception as E:
    print E

We can use this method in this way:

Copy Code code as follows:
Scp_by_key (' 192.168.0.33 ', '/xx/xxx/a.txt ', ' xx/xxx/b.txt ', ' Xiaomo ', '/home/xiaomo/.ssh/id_rsa ')

Isn't it cool to use it? But the premise is to have the RSA key ... If you need a password, simply swap the Pkey parameter for the password pass:

t = Paramiko. Transport ((HOST_IP, Host_port))
t.connect (username=username, password= ' xxx ')

More information about Python-related content can be viewed in this site: "Python file and directory operation tips Summary", "Python text file Operation tips Summary", "Python URL operation tips Summary", "Python Picture Operation tips Summary", " Python data structure and algorithm tutorial, Python socket Programming Tips Summary, Python function Usage tips Summary, python string manipulation tips and Python introductory and advanced classic tutorials

I hope this article will help you with Python programming.

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.