1

I am trying to upload a file via SFTP to my server. But instead of just uploading it, I have to explicitly tell my script what file to overwrite on the server. I don't know how to change that.

#!/usr/bin/python3
import paramiko
k = paramiko.RSAKey.from_private_key_file("/home/abdulkarim/.ssh/id_rsa")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("connecting")
c.connect( hostname = "do-test", username = "abdulkarim", pkey = k )
print("connected")
sftp = c.open_sftp()
sftp.put('/home/abdulkarim/Skripte/data/test.txt', '/home/abdulkarim/test/test1.txt')
c.close()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

2

In the below call, the second (remotepath) parameter refers to the path, where the file will be stored on the server. There is no requirement for the remote file to actually exist. It will be created.

sftp.put('/home/abdulkarim/Skripte/data/test.txt', '/home/abdulkarim/test/test1.txt')

Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server"

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992