0

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?

John_F
  • 101
  • 1
  • 2
  • 10
  • I suggest to use ssh_keys instead of written passwords. Your life will be easier and at the same time more secure. – mosh442 Mar 02 '17 at 14:20
  • Unfortunately, that's not always an option. I have an app that needs to connect to a remote system for which I can't upload keys. – Edward Falk Jun 25 '18 at 18:57

2 Answers2

0

The password prompt isn't written to stdout. You'll need something like pexpect to capture it. This thread has more info: Sending a password over SSH or SCP with subprocess.Popen

Edit: Regarding your updated pexpect code, first of all, if you read the documentation for subprocess, you'll see that communicate() waits for the process to terminate, that's why your program hangs until you kill the ssh session. And your call to pexpect.spawn looks wrong, it expects a string, not a Popen object. You don't need subprocess at all here.

Community
  • 1
  • 1
  • I have adjusted my code (above) to include your suggestion but I'm still having the same problems. – John_F Mar 02 '17 at 16:09
0

With guidance from Johannes Holmberg:

import pexpect

prompt = "cisco_prompt"

def start_log():
        global child
        child.logfile = open("/tmp/mylog", "w")

def stop_log():
        global child
        f = open('/tmp/mylog', 'r')
        for line in f:
                cleanedLine = line.strip()
                if cleanedLine: # is not empty
                        print(cleanedLine)
        f.close()
        child.logfile.close

ssh_cmd = "ssh -A -t username1@200.1.2.3 ssh -A -X username2@10.1.2.3"
child = pexpect.spawn(ssh_cmd, timeout=30)
print "Waiting for 1st password prompt"
child.expect ('password')
child.sendline ('password1')
print "Waiting for 2nd password prompt"
child.expect ('password')
child.sendline ('password2')
start_log()
child.expect (prompt)
child.sendline ('conf t')
stop_log()
start_log()
child.expect ('(config)')
stop_log()
print "Ready for more code"
child.close
print "END"
John_F
  • 101
  • 1
  • 2
  • 10