0
import subprocess
import os
p = subprocess.Popen(["scp", "test.txt", "root@x.x.x.x:/workspace/waic/data"])
sts = os.waitpid(p.pid, 0)

print('Done')

I got this message:

Host key verification failed.
lost connection

I want to copy files from a remote machine, but I don't know how to add a '-p 33' port argument. If I add it after 'scp -p 33', it gives another error:

No such file or directory: 'scp -p 33'
marlon
  • 6,029
  • 8
  • 42
  • 76
  • https://stackoverflow.com/questions/10341032/scp-with-port-number-specified – bzu Sep 02 '22 at 18:03
  • BTW, if you want to always immediately wait for the process to exit, why not use `subprocess.call()` or `subprocess.run()`? – Charles Duffy Sep 02 '22 at 18:09
  • 1
    You show the command that does work but don't show the one that doesn't. Show us the non-working stuff instead. – tdelaney Sep 02 '22 at 18:09
  • "Host key verification failed." could mean that you haven't added this host to your local ssh configuration. ssh-add or ssh-copy-id may be in order. But that's more a http://superuser.com question. – tdelaney Sep 02 '22 at 18:14

2 Answers2

1

Those are just additional parameters on the command line. Also fixing the flag "-P" (assuming you are using a standard unixlike scp):

p = subprocess.Popen(["scp", "-P", "33", "test.txt", "root@x.x.x.x:/workspace/waic/data"])
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • @CharlesDuffy - Just fixing the bug at hand, but I'll change that too. – tdelaney Sep 02 '22 at 18:07
  • how to pass the password argument to the command? – marlon Sep 02 '22 at 18:14
  • @marlon - Good question! But https://superuser.com is a better place for it. You could leave out the python stuff and just try getting it to work on the command line. And ask questions there as needed. – tdelaney Sep 02 '22 at 18:15
  • @marlon - The python paramiko package https://www.paramiko.org/ is a great alternate because its a pure python package that knows scp and etc... – tdelaney Sep 02 '22 at 18:18
  • My real task is more complex so it's much more convenient using python than shell. But I have to start with this first issue. – marlon Sep 02 '22 at 18:18
  • 1
    @marlon - its possible to do server to server copies without a password by sharing the keys. This is considered safer than hardcoding a password in a python script. The only reason for getting it going on the command line is to have a better way to ask superuser.com. – tdelaney Sep 02 '22 at 18:20
1

You have 3 issues.

  1. You are not typing your arguments into the list of args correctly. Below is an example of how to properly do so.
  2. You have incorrect casing on your port argument.
  3. You are trying to scp but it is expecting a password, this should help.
import subprocess
import os
p = subprocess.Popen(["sshpass", "-p", "YOUR PASSWORD HERE",  "scp", "-P", "33", "test.txt", "root@x.x.x.x:/workspace/waic/data"])
sts = os.waitpid(p.pid, 0)

print('Done')
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
JRose
  • 1,382
  • 2
  • 5
  • 18
  • The OP doesn't show _how_ they did it, but it looks to me like their quoting was wrong, not just their casing. Otherwise it wouldn't be the entire string `scp -p 33` being treated as a filename. – Charles Duffy Sep 02 '22 at 18:06
  • That's one of the bugs - but since python can't find a file called "scp -p 33", the parameter were not filled out correctly. – tdelaney Sep 02 '22 at 18:07
  • But why do I still get the 'Permission denied, please try again.' after I follow your suggestion? – marlon Sep 02 '22 at 18:10
  • @marlon, we only answer one question at a time. If you have a new and different problem, ask a new and different question. – Charles Duffy Sep 02 '22 at 18:10
  • @CharlesDuffy I think the problem is the 'password' argument? How to pass that argument in the command? There is no password argument in the Popen argument. – marlon Sep 02 '22 at 18:12
  • @CharlesDuffy You are correct that something other than their casing was wrong, however, tdelaney and I independently came up with the exact same way to write this line (which I tested and it seems to work?) so I believe that my answer still answers the OPs question. – JRose Sep 02 '22 at 18:13
  • @marlon Try my updated code snippet. – JRose Sep 02 '22 at 18:15
  • 1
    I agree that it's not a _bad_ answer (it does demonstrate the necessary fix!), but unless the prose describes _why_ things had to be changed (so the OP can update their mental model of how command lines are processed without needing to infer from comparing the example to their original code), I don't personally consider it a good enough one to upvote; hence the comment above nudging towards such an edit. – Charles Duffy Sep 02 '22 at 18:17
  • @JRose, now i got this error: ' [Errno 2] No such file or directory: 'sshpass'' – marlon Sep 02 '22 at 18:20
  • @CharlesDuffy you are correct, I should have done a better job explaining to OP what they were doing wrong. – JRose Sep 02 '22 at 18:20
  • @marlon do sudo apt install sshpass – JRose Sep 02 '22 at 18:21