-1

Trying to login to a Cisco router. It's not sending the right commands at the "--More--" prompt. I think it might have to do on what I'm sending. I need to have a space command sent when it gets to the More prompt.

It will send a "\" character, but will net send a " " or any random letter either.

Current Code:

import pexpect
import sys
import os
import getpass

hostname = "router-2"
username = raw_input('Enter Your username: ')
password = getpass.getpass('Password:')

fout = file('agglog.txt','w')

child = pexpect.spawn('ssh %s@%s' % (username, hostname))
child.logfile_read = fout
child.expect('Password:')
child.sendline(password)
child.expect('>')
child.sendline('enable')
child.expect('Password:')
child.sendline(password)
child.expect('#')
child.sendline('show processes cpu history')
i = child.expect(["--More--","#"])
if i==0:
    child.sendline(' ') ###PROBLEM IS HERE
else:
    child.sendline('show processes cpu | i Core 0|Core 1')
child.expect('#')
child.sendline('logout')

Again, at the --MORE-- prompt it just hangs like it's waiting for something when trying to send a space command. On Cisco routers, when you press space at that prompt it displays more output. "terminal length 0" is not an option in this case due to permissions.

Any help is greatly appreciated.

Edit:

Here is what the output looks like:

router-2#show processes cpu history

History information for system:


    111112222222222111111111122222111111111122222111111111111111
    777770000000000999998888833333888889999900000999998888899999
100                                                             
 90                                                             
 80                                                             
 70                                                             
 60                                                             
 50                                                             
 40                                                             
 30                                                             
 20 ************************************************************
 10 ************************************************************
   0....5....1....1....2....2....3....3....4....4....5....5....
             0    5    0    5    0    5    0    5    0    5    
               CPU% per second (last 60 seconds)


    222222222222222222221222222222222222222222222222222222222222
    342000300104111121019410422602031010060202210143001042120601
 --More-- 
Dan
  • 359
  • 5
  • 17

2 Answers2

0

I'm assuming pexpect is timing out just before the last line, because it's expecting #, instead it's getting another --More--? Think you need to use a while loop, something like this maybe:

while count < 10:
    output = child.readline()
    if '--More--' in output:
        child.sendline(' ')
    elif '#' in output:
        child.sendline('logout')
    count +=1
    time.sleep(1)

It's also good to sleep for one second before each loop to give the terminal time to output the next page.

Hope this help.

Edit:

Hmm that was kinda weird, expect and readline unable to parse --More-- but it's in pexpect buffer and it sees it. I've tested this and it works from me:

child.sendline('show processes cpu history')
count = 0
while count < 100:
output = child.readline().strip()
if '--More--' in child.buffer:  ### read buffer like so
    print output
    child.sendline(' ')
elif '#' in output:
    print 'done'
    child.sendline('logout')
    break
count +=1

If you try to print child.buffer you will notice the router terminal sends a lot of them so you might need to adjust your loop count.

0

For anyone who stumbles across this thread, I have dodged this issue in the past by first sending the command:

terminal length 0

This stops the devices from 'paging' the output so you can stop and read it.

The command is only relevant for your currently opened session and disappears after disconnection.

Matt
  • 1