0

I want to save a file from my laptop to a remote server using python but i get the error : OSError: Failure from the second last command.

import paramiko
ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='ipaddress',username='user',password='passwd', port='port')

ftp_client=ssh_client.open_sftp()
ftp_client.put('laptop/path/to/file/','server/saving/path/')
ftp_client.close()

Additionally, because a file with a same name already exists on that file i want to replace it with this one. Any ideas why this code is not working?

I do not want to use ssh on my terminal because i want the python script to automatically do this every week while the file is updated.

Thank you in advance

2
  • Please provide the entire error message. Commented Mar 16, 2020 at 21:10
  • OSError: Failure This was only the error message. But i solved the problem. Thanks a lot! @AMC Commented Mar 16, 2020 at 22:59

1 Answer 1

1

You can use a scp package to do secure copy up to and from the remote server. Something like this.

from scp import SCPClient

....

scp = SCPClient(ssh_client.get_transport())
payload = os.path.join(...path to file)
scp.put(payload, '/path/on/server/')
Sign up to request clarification or add additional context in comments.

3 Comments

if i change the last command to scp.put(payload, 'saving/directory/on/server/') the file will be saved on the wanted directory on the server replacing the already existing file with the same name?
@EAS the payload is the path to a directory or file that you want to send up to the server, you can specify the remote path for your upload to go in, or in the case of uploading a file, you can specify a remote path and file name. Such as scp.put('test.txt', '~/test2.txt') for instance.
Ok, it seems that you solved my problem. Thanks a lot!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.