开发者

Python - telnet on routers and list full result (hitting space bar)

import telnetlib

def telNetCall():
    host  = "10.200.1.23"
    user  = "me"
    password = "matrix"
    telnet  = telnetlib.Telnet(host) 
    telnet.read_until('Username: ', 3) 
    telnet.write(user + '\r')
    telnet.read_until('Password: ', 3)  
    telnet.write(password + '\r') 
    telnet.write("sh log"+ "\r\n")
    telnet.write('exit' + '\r')
    print telnet.read_all() 

My problem is that when I try to grab "show log" from cisco router, it gives me just partial result, I 开发者_StackOverflow中文版thing it is because you need to hit space bar 5 times (depends how long is log history) to get full log-when I do it manualy, and I don't know how to tell python to list full log. Any suggestion?


Not a solution, but seems a plausible workaround:

You can disable terminal paging by issuing the following IOS CLI command.

terminal length 0


It actually is the exact answer ..... you have to enter terminal length 0 or the router pages the output like using unix "more" so that it's human readable and doesn't just scroll by to quickly

import telnetlib

def telNetCall():
    host  = "10.200.1.23"
    user  = "me"
    password = "matrix"
    telnet  = telnetlib.Telnet(host) 
    telnet.read_until('Username: ', 3) 
    telnet.write(user + '\r')
    telnet.read_until('Password: ', 3)  
    telnet.write(password + '\r') 

    telnet.write("term length 0"+ "\r\n")

    telnet.write("sh log"+ "\r\n")
    telnet.write('exit' + '\r')
    print telnet.read_all() 


The problem then becomes how to capture output larger than about 20kb... when logging to file I get all the output but I only want to save selective output, not the entire session. When using child.after the output is truncated, eg the beginning is missing which can be mitigated a little by increasing maxread but it still bombs out when getting close to 30-40kb of output.

For those wondering, I'm grabbing the output of Cisco switches using "sh int | i protocol|Last" this only returns 3 lines per interface but some of my customer's switch stacks have 450 ports.

Not sure what the dynamics are here but for some switches it does succeed.

For a while I tried to work around the issue by selectively turning logging to file on and off. This proved unreliable and from the generic logging I can see that pexpect matches output way before the logging sees it. I ended up disabling logging while data was still being written to file. truncating the output after about 10 lines of output.

So the ultimate question remains, how can one increase the reliability of child.after. In order to capture large outputs. I have no issue with increasing the timeout on expect statements, as long as the output is reliable. What I see now is no timeout with incomplete output.

child.sendline(action)
child.expect(prompt_keys)
action_output = str(child.after)    # Captures the output
# print action_output   # Uncomment for debugging grabbed output
output_file.write(action_output + "\n\n\n") # Write the output to file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜