簡體   English   中英

如何在paramiko的ssh連接中將文件傳輸到ssh服務器?

[英]How to transfer a file to ssh server in an ssh-connection made by paramiko?

我使用Python的paramiko數據包來保持與服務器的ssh連接:

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("xxx.xxx.xxx.xxx",22,username=xxx,password='',timeout=4)

我想用這個ssh-connection將文件傳輸到ssh服務器,我該怎么辦?

就像使用scp a-file xxx@xxx.xxx.xxx.xxx:filepath命令一樣?

嘗試這個:

s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect("xxx.xxx.xxx.xxx",22,username=xxx,password='',timeout=4)

sftp = s.open_sftp()
sftp.put('/home/me/file.ext', '/remote/home/file.ext')

以下是來自https://www.programcreek.com/python/example/4561/paramiko.SSHClient的另一個示例

def copy_file(hostname, port, username, password, src, dst):
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    print (" Connecting to %s \n with username=%s... \n" %(hostname,username))
    t = paramiko.Transport(hostname, port)
    t.connect(username=username,password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    print ("Copying file: %s to path: %s" %(src, dst))
    sftp.put(src, dst)
    sftp.close()
    t.close() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM