2

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.

2
  • What are you doing with putty? It might be a better idea to do the task directly from a python script (for example, to execute remote commands via SSH you could use paramiko). Commented Jul 28, 2016 at 16:54
  • I'm not sure what your requirements are, but you can ssh and give commands at the same time: ssh [email protected] 'ls -l; ps -aux; whoami' with more info here
    – mitoRibo
    Commented Jul 28, 2016 at 17:03

1 Answer 1

0

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.

1
  • 1
    I 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) using Popen. The problem isn't opening putty, the problem is getting putty to do what OP wants once it is opened...
    – mgilson
    Commented Jul 28, 2016 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.