Using subprocess to execute a linux command in python and then grab the resulting prompt -


i using subprocces.popen() run linux commands in python. trying use command ssh different machine, , see whether prompts me password.

however, have no idea how grab resulting prompt , use in python script.

for example, if use subprocess.popen() call command "echo hello world" can use communicate() method grab resulting stdout running command.

but here trying do: use popen() run: ssh -hostname- 'echo hello' , grab result of use within python script. if ssh runs prompt password, want able have output of prompt in python script use string. finally, want able exit out of prompt (equivalent of typing ^c when using linux terminal). however, when using popen() , communicate(), shows prompt on actual linux stdout , doesn't grab prompt.

i hoping suppress prompt ever showing on actual linux terminal stdout.

here code 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, password prompt not stored in sshoutput, issue. if ran following command, result string containing "hello"

p = subprocess.popen(["echo", "hello"], stdout=subprocess.pipe, stderr=subprocess.pipe) result, err = p.communicate() 

the problem ssh prints prompt , reads password current tty, not stdout/stdin. see this question.

possible solutions:

  • connect ssh's stdin , stdout slave pty , read/write master pty application. can use expect or pexpect (in python).
  • run ssh inside wrapper program insert pty between it's stdin/stdout , ssh's stdin/stdout. maybe unbuffer work. in case can continue using subprocess.
  • use sshpass. can use subprocess.

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -