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?
Popen? - alexisshell=Truewhere possible, such as in this case. Maybe putty.exe returns a more useful exit code than the shell's exit code of "1" (fail). - Eryk Sunenv, generally you should modifyos.environ.copy(). The docs clearly state what this parameter does: "If env is notNone, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. Note: If specified,envmust provide any variables required for the program to execute. On Windows, in order to run a side-by-side assembly the specifiedenvmust include a validSystemRoot." The crypto API also requiresSystemRoot. - Eryk Sun