Paramiko's SSHClient with SFTP

后端 未结 2 1920
日久生厌
日久生厌 2020-11-29 18:47

How I can make SFTP transport through SSHClient on the remote server? I have a local host and two remote hosts. Remote hosts are backup server and web server. I

2条回答
  •  隐瞒了意图╮
    2020-11-29 19:21

    paramiko.SFTPClient

    Sample Usage:

    import paramiko
    paramiko.util.log_to_file("paramiko.log")
    
    # Open a transport
    host,port = "example.com",22
    transport = paramiko.Transport((host,port))
    
    # Auth    
    username,password = "bar","foo"
    transport.connect(None,username,password)
    
    # Go!    
    sftp = paramiko.SFTPClient.from_transport(transport)
    
    # Download
    filepath = "/etc/passwd"
    localpath = "/home/remotepasswd"
    sftp.get(filepath,localpath)
    
    # Upload
    filepath = "/home/foo.jpg"
    localpath = "/home/pony.jpg"
    sftp.put(localpath,filepath)
    
    # Close
    if sftp: sftp.close()
    if transport: transport.close()
    

提交回复
热议问题