I'm trying to run a script that runs putty and within the putty terminal that gets created, run a command. I've been able to start putty from my script using Python's subprocess module with check_call or Popen. However, I'm confused as to how I can run a command within the subprocess Putty terminal from my script. I need to be able to run this command in putty and analyze its output on the Putty terminal. Thanks for any help.
1 Answer
You need to set the stdin
argument to PIPE
and use the communicate
function of Popen
to send data to stdin.
from subprocess import Popen, PIPE
p = Popen('/the/command', stdin=PIPE, stdout=PIPE, stderr=PIPE)
std_out, std_err = p.communicate('command to putty')
That being said, it may be easier to use a python library that implements the ssh
protocol (like paramiko
) rather than going through putty.
-
1I would consider this to be 1. A link only answer. 2. Not particularly helpful since OP seems to already know how to start an external program (
putty
) usingPopen
. The problem isn't openingputty
, the problem is gettingputty
to do what OP wants once it is opened...– mgilsonCommented Jul 28, 2016 at 16:57
ssh [email protected] 'ls -l; ps -aux; whoami'
with more info here