python 使用paramiko模塊上傳本地文件到ssh


我們要了解幾個函數:

paramiko.Tranport(("目標ip,端口"))#這是上傳目標的IP和端口

  

paramiko.SFTPClient.from_tranport()#安全文件傳輸憑證

  

sftp.put(loaclfile,remotefile)#loaclfile是要上傳的文件,remotefile是上傳后要保存的文件名

  代碼版:

import paramiko
ssh=paramiko.Transport(("192.168.223.129",22))
ssh.connect(username="root",password="root")
sftp=paramiko.SFTPClient.from_transport(ssh)
localfile="使用說明.txt"
remotefile="使用說明.txt"
try:
  sftp.put(localfile,remotefile)
except Exception:
    print("[-]put Error:User name or password error or uploaded file does not exist")


print("上傳")
ssh.close()

成功圖:

轉載請注釋:來自本地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM