Question

I'm using pepexpect to login into a router. The way I do it is by login into a linux server and then to the routers.

usr = 'myusername'
pwd='mypwd'
child.sendline('ssh -o StrictHostKeyChecking=no -l '+usr+' '+ip)
index = child.expect(['assword:', pexpect.EOF,pexpect.TIMEOUT])

When I login into the router manually, I get the following:

WARNING NOTICE: This is a private system. The actual or attempted, unauthorized
access, use or modification of this system is strictly prohibited.
Individuals undertaking such unauthorized access, use or modification are
subject to company disciplinary proceedings and/or criminal and civil penalties
under applicable domestic and foreign laws. The use of this system may be
monitored and recorded for administrative and security reasons in accordance
with local law. If such monitoring and/or recording reveals possible evidence
of criminal activity, the results of such monitoring may be provided to law
enforcement officials. Continued use of this system after receipt of this
notice constitutes consent to such security monitoring and recording.
!
Global Baseline Configuration:  v1.0

Cisco Wide Area Application Engine

username@10.58.218.237's password: 

Even thought the password is on the output the index result is 2 = pexpect.TIMEOUT My script work onb other routers, but I don't know why is not working on this one. Thanks

Was it helpful?

Solution

I've had a hard time using pexpect in order to fill prompts but I've managed to write something working:

def call_and_type(command, prompt_regex, entry):
    p = pexpect.spawn(command, logfile=sys.stdout, maxread=16384)
    index = p.expect_exact([prompt_regex, pexpect.EOF])
    if index == 0:
        p.setecho(False)
        p.sendline(entry)
        while p.read():
            pass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top