Secure Copy File from remote server via scp and os module in Python

Posted by user1063572 on Stack Overflow See other posts from Stack Overflow or by user1063572
Published on 2011-11-24T09:38:18Z Indexed on 2011/11/24 9:52 UTC
Read the original article Hit count: 242

Filed under:
|
|

I'm pretty new to Python and programming. I'm trying to copy a file between two computers via a python script. However the code

os.system("ssh " + hostname + " scp " + filepath + " " + user + "@" + localhost + ":" cwd)

won't work. I think it needs a password, as descriped in How do I copy a file to a remote server in python using scp or ssh?. I didn't get any error logs, the file just won't show in my current working directory.

However every other command with os.system("ssh " + hostname + "command") or os.popen("ssh " + hostname + "command") does work. -> command = e.g. ls

When I try ssh hostname scp file user@local:directory in the commandline it works without entering a password.

I tried to combine os.popen commands with getpass and pxssh module to establish a ssh connection to the remote server and use it to send commands directly (I only tested it for an easy command):

import pxssh
import getpass

ssh = pxssh.pxssh()
ssh.force_password = True
hostname = raw_input("Hostname: ")
user = raw_input("Username: ")
password = getpass.getpass("Password: ")
ssh.login(hostname, user, password)
test = os.popen("hostname")
print test

But I'm not able to put commands through to the remote server (print test shows, that hostname = local and not the remote server), however I'm sure, the conection is established. I thought it would be easier to establish a connection than always use "ssh " + hostname in the bash commands. I also tried some of the workarounds in How do I copy a file to a remote server in python using scp or ssh?, but I must admit due to lack of expirience I didn't get them to work.

Thanks a lot for helping me.

© Stack Overflow or respective owner

Related posts about python

Related posts about ssh