cool77
cool77

Reputation: 1136

Python code to scp a file fails with name resolution issue

I am trying to scp a file from my local linux machine to a remote linux machine. Here is the code that i am trying

filename = '/tmp/myfile'
remotepath = '/tmp'
command = 'nodeattr'
sproc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
server = sproc.communicate()

print "connecting to",server[0]

p = subprocess.Popen(["scp", filename, "root@"+server[0]+":"+remotepath])
sts = p.wait()

When I run the above, I get an error as below

connecting to abc.local

ssh: Could not resolve hostname abc.local : Name or service not known
lost connection

However, when I do a ssh on command line, it allows me to connect to the remote host abc.local and I don't have an issue. But when I am using subprocess, I am getting this error.

Any idea if I am using the subprocess in a wrong way?

Upvotes: 0

Views: 321

Answers (1)

jasonharper
jasonharper

Reputation: 9622

Notice the line break between "abc.local" and the following colon in the error message: the actual string you got back from the first Popen is "abc.local\n". You need to .strip() or otherwise get rid of that newline character.

Upvotes: 1

Related Questions