user13475995
user13475995

Reputation:

Upload new file to SFTP server using Paramiko without having to overwrite an existing file

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()

Upvotes: 1

Views: 3552

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202158

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"

Upvotes: 2

Related Questions