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.