I have the following code (updated to include pexpect):
import sys
import subprocess
import pexpect
print "0"
ssh = subprocess.Popen("ssh -A -t username1@200.1.2.3 ssh -A -X username2@10.1.2.3",
shell = True,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE).communicate()
print "1"
child = pexpect.spawn(ssh)
print "2"
child.expect ('password')
print "3"
child.sendline ('password2')
print "4"
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result
When I run it, I see the Zero printed followed by the Password prompt displayed on the screen. The line that prints One never executes and so the following pexpect code doesn't either. I can enter the password as a user but then it hangs. When I kill it with Ctrl+C, the 2nd login banner with 2nd Password prompt then appears before returning to the command prompt. Can someone please explain how to capture the 1st Password prompt so the program can send the password instead of the user? Also, can someone please explain why I don't get the result variable until I kill the program?