5

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 to 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 to 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.

4
  • 2
    Why do you think you need to do ssh hostname scp? Why not just scp user@hostname:file localdirectory? Commented Nov 24, 2011 at 9:54
  • Prefer the subprocess module over os.system() for security reasons. Commented Nov 24, 2011 at 10:09
  • I haven't thought about the posibility to copy via scp user@hostname:file localdirectory. It does work, but only when I cancel the user@ part. Commented Nov 24, 2011 at 10:39
  • I didn't know the subprocess module until now, but I will take it into consideration (as I said I'm new to python and learning, I need some time to adopt) Commented Nov 24, 2011 at 10:42

1 Answer 1

4

I think the easiest (to avoid having to enter a password) and most secure way to go about this is to first set public/private key authentication. Once that is done, and you can log in to the remote system by doing ssh user@hostname, the following bash command would do the trick:

scp some/complete/path/to/file user@remote_system:some/remote/path

The corresponding Python code would be:

import subprocess

filepath = "some/complete/path/to/file"
hostname = "user@remote_system"
remote_path = "some/remote/path"

subprocess.call(['scp', filepath, ':'.join([hostname,remote_path])])
Sign up to request clarification or add additional context in comments.

Comments

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.