1

I am using subprocces.Popen() to run linux commands in python. I am trying to use this command to ssh into a different machine, and see whether it prompts me for a password.

However, I have no idea how to grab the resulting prompt and use it in the python script.

For example, if I use subprocess.Popen() to call the command "echo Hello World" I can then use the communicate() method to grab the resulting stdout from running that command.

But here is what I am trying to do: use Popen() to run: ssh -hostname- 'echo Hello' and then grab the result of that for use within the python script. So if the ssh runs into a prompt for the password, I want to be able to have the output of that prompt in the python script to use as a string. Finally, I want to be able to exit out of this prompt (equivalent of typing ^C when using linux terminal). However, when using Popen() and communicate(), it simply just shows the prompt on the actual linux stdout and doesn't grab the prompt.

I was also hoping to suppress this prompt from ever showing on the actual linux terminal stdout.

Here is the code that I have been using (this code works when grabbing normal stdout):

p = subprocess.Popen(["ssh", hostName, "echo Hello"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sshOutput, err = p.communicate()

Of course, the password prompt is not stored in sshOutput, which is the issue. If I ran the following command, result would be a string containing "Hello"

p = subprocess.Popen(["echo", "Hello"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result, err = p.communicate()
OMGitzMidgar
  • 689
  • 2
  • 10
  • 28
  • sounds like you want pexpect but can you give an actual example of what output you are talking about? – Padraic Cunningham Jul 21 '15 at 21:43
  • I edited the question with a full explanation of what I am trying to do and what it actually does. – OMGitzMidgar Jul 21 '15 at 21:50
  • Sorry, I still don't quite understand. what exactly are you tryng to get in relation to *So if the ssh runs into a prompt for the password, I want to be able to have the output of that prompt in the python script to use as a string* – Padraic Cunningham Jul 21 '15 at 21:57
  • I believe OP is asking to redirect stdout, so that the script can parse "Password: " etc. – rabbit Jul 21 '15 at 22:07
  • Assuming passwordless ssh isn't set up, when you try to ssh into another machine you are prompted for a password first. I want to be able to take the text of that prompt and use it in the python script that called the ssh command. Just like if I had a python script that ran the command "ls" and then grabbed the output and used it within the script, I want to be able to grab that password prompt output. – OMGitzMidgar Jul 21 '15 at 22:18
  • Ok so you mean the code waits because you are stuck at the password prompt? – Padraic Cunningham Jul 21 '15 at 22:42
  • Yes, exactly! The code waits because there is a password prompt, and when you run the code you can even see the password prompt in the terminal. My goal is to redirect that password prompt to use within the script as a variable just like I can redirect any other stdout. This is to avoid the code getting hung up on the prompt and to analyze the text of the prompt. – OMGitzMidgar Jul 21 '15 at 22:46
  • so basically you are trying to get something like `user@localhost's password:`? – Padraic Cunningham Jul 21 '15 at 22:49
  • Yes, exactly! I know that seems silly, but that is what I am trying to do. I don't want "user@localhost's password: " to be a prompt on the terminal that is making the rest of the code wait, instead I want to redirect that prompt into a variable in the running python script just like I could with any other stdout using subprocess. – OMGitzMidgar Jul 21 '15 at 22:53
  • Looks like there might be some useful stuff at [this answer.](http://stackoverflow.com/questions/21433660/how-to-interact-with-ssh-using-subprocess-module) I imagine pexpect would be very useful here. – rabbit Jul 21 '15 at 22:58

1 Answers1

2

The problem is that ssh prints prompt and reads password from current tty, not stdout/stdin. See this question.

Possible solutions:

  • Connect ssh's stdin and stdout to a slave pty and read/write master pty from your application. You can use expect or pexpect (in Python).
  • Run ssh inside some wrapper program that will insert a pty between it's stdin/stdout and ssh's stdin/stdout. Maybe unbuffer will work. In this case you can continue using subprocess.
  • Use sshpass. You can also use this with subprocess.
Community
  • 1
  • 1
gavv
  • 4,649
  • 1
  • 23
  • 40