Transfer files/folders across servers using paramiko

Some concepts
SSH (Secure Shell)
is a security protocol based on the application layer , which provides security for remote login and other network services.
SSH File Transfer Protocol (Secret File Transfer Protocol, SFTP, Secure File Transfer Protocol) is a data stream connection, a network transfer protocol that provides file access, transfer and management functions.

SSHClient is similar to Linux's ssh command, it is the encapsulation of SSH session, this class encapsulates the transport (Transport), the channel (Channel) and the SFTPClient establishment method (open_sftp).
SFTPClient is similar to Linux's sftp command. It is a package for SFTP client to realize remote file operations, such as uploading, downloading, and modifying file permissions.

Paramiko basic noun
Channel: It is a kind of Socket, a secure SSH transmission channel;
Transport: It is an encrypted session, and an encrypted Tunnels will be created synchronously when used, and the Tunnels are called Channel;
Session: It is Client (client program) The object that maintains the connection with the Server (server), start the session with connect()/start_client()/start_server().

Basic use of Paramiko

  1. Introduction to the commonly used methods of SSHClient
    (1) connect(): To realize the connection and authentication of the remote server, only the hostname is a required parameter for this method.
    Common parameters
    hostname Connected target host
    port=SSH_PORT Specify port
    username=None authenticated user name
    password=None authenticated user password
    pkey=None private key method is used for authentication
    key_filename=None A file name or file list, specify the private key file
    timeout=None optional tcp connection timeout
    allow_agent=True, whether to allow connection to ssh agent, the default is True allow
    look_for_keys=True whether to search for private key files in ~/.ssh, the default is True allow
    compress=False, whether Open compression
    (2) set_missing_host_key_policy(): Set the response policy when the remote server is not recorded in the know_hosts file. Currently supports three policies:
    setting the policy when the connected remote host does not have a local host key or a HostKeys object. Currently, three policies are supported:
    AutoAddPolicy Automatically add the host name and host key to the local HostKeys object, independent of the configuration of load_system_host_key. That is, when a new ssh connection is established, you do not need to enter yes or no for confirmation
    . WarningPolicy is used to record a python warning of an unknown host key. And accept, similar in function to AutoAddPolicy, but will prompt a new connection
    RejectPolicy automatically rejects unknown hostnames and keys, depending on the configuration of load_system_host_key. This is the default option
    (3) exec_command(): A method to execute Linux commands on a remote server.
    (4) open_sftp(): Create an sftp session based on the current ssh session. This method returns an SFTPClient object.


# Two modules need to be installed first 
# conda install -c conda-forge paramiko 
# conda install -c conda-forge scp 
import paramiko
from scp import SCPClient
# Establish SSH connection 
ssh  = paramiko.SSHClient ( ) 
# Allow connections to hosts not in the know_hosts file 
ssh.set_missing_host_key_policy ( paramiko.AutoAddPolicy ( )) 
ssh.connect ( hostname = '' , port = 22, username = ' name' , password = '1234567' ) 
# SCPClient uses paramiko transport as parameter 
scp  = SCPClient ( ssh.get_transport ( ))

# Download TESTdir and its contents from the remote to the current host path 
scp.get ( '/home/remote_path/TESTdir' , '/home/local_path/' , recursive = True ) 
# Upload excel2 and its contents to the remote In the end host directory 
scp.put ( '/home/local_path/excel2' , '/home/remote_path/TESTdir' , recursive = True ) 
# So the first path = the path of the file/folder to be operated 
# The second path = where the file/folder is going 
scp.close ( ) 
ssh.close ( )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22


Note: If you want to put recursive=True in the middle as in the example on the official website, the following error will be reported in actual operation:
SyntaxError: positional argument follows keyword argument

Add an example (scp) a file (or folder) on a remote server with Python.html?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog -BlogCommendFromBaidu-3.control

Related: Transfer files/folders across servers using paramiko