简体   繁体   中英

Connecting to remote linux machine via python subprocess using PuTTY or plink throws error

I am trying to execute few scripts in remote linux machine from windows host machine. I am hoping to achieve this using python subprocess +putty/plink. When I try Putty or plink commands from windows cmd, it works fine. But if I try the same command using python subprocess, I get a lot of errors.

C:\Users\username>plink.exe username@machinename -pw password

Works fine. But when I try from python,

process = subprocess.Popen('plink.exe username@machinename -pw password'.split(),
                           env={'PATH':'C:\\Program Files (x86)\\PuTTY\\'},
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)

Throws the following error.

Unable to open connection:

gethostbyname: unknown error'

process = subprocess.Popen("putty.exe -ssh -2 -l username -pw password -m C:\\script.sh machinename",
                           env={'PATH':'C:\\Program Files (x86)\\PuTTY\\'},
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT
                           ,shell=True);

Unable to open connection:

gethostbyname: unknown error'

I tried subprocess.check_ouput too with no luck.

output = subprocess.check_output("putty.exe -ssh -2 -l username -pw password -m C:\\script.sh machinename", stderr=subprocess.STDOUT,shell=True)

Throws the following error

CalledProcessError: Command 'putty.exe -ssh -2 -l username -pw password -m C:\\script.sh machinename' returned non-zero exit status 1

Could this be a firewall issue?

I highly advise against using PuTT or in general every external program to connect to shh and then interface with pipes.

Using the python library paramiko this can be done much better.

For example:

# ... connect like one of the examples on github
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
    print '... ' + line.strip('\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM