15

I have read over this post extensively and have researched Exscript, paramiko, Fabric and pxssh and I am still lost Persistent ssh session to Cisco router . I am new to python scripting.

I am attempting to write a script in Python that will SSH into a Cisco device, run "show version", display the results in notepad, then end the script.

I can get this working with show commands that do not require the user to interact with the device. For example:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              
conn = SSH2()                       
conn.connect('192.168.1.11')     
conn.login(account)                 

conn.execute('show ip route')
print conn.response

conn.send('exit\r')               
conn.close()                        

The above script will display the results of "show ip route".

If I try conn.execute('show version') the script times out because the Cisco device is expecting the user to press space bar to continue, press return to show the next line or any key to back out to the command line.

How can I execute the show version command, press space bar twice to display the entire output of the show version command, then print it in python?

Thank you!!!!

3 Answers 3

21

Try executing terminal length 0 before running show version. For example:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              
conn = SSH2()                       
conn.connect('192.168.1.11')     
conn.login(account)  

conn.execute('terminal length 0')           

conn.execute('show version')
print conn.response

conn.send('exit\r')               
conn.close()  

From the Cisco terminal docs: http://www.cisco.com/en/US/docs/ios/12_1/configfun/command/reference/frd1003.html#wp1019281

1
  • Thank you! I was looking at this the complete wrong way. Thank you for helping me look outside the box I was in :)
    – D3l_Gato
    Aug 21, 2011 at 20:37
7

First execute

terminal length 0

to disable paging.

0
2

I just asked the same thing and the below code will run from a list and obtain the information you are asking for.

from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\NewdayTest.txt','w') # Where you want the file to save to.
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'username' # edit to reflect
password = 'password' # edit to reflect

ip_add_file = open(r'C:\IPAddressList.txt','r') # a simple list of IP addresses you want to connect to each one on a new line

for host in ip_add_file:
    host = host.strip()
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable') #Editable to be what ever is needed
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
    output = device.send_command('sh run')
    print(output)
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
    output = device.send_command('sh ip int br')
    print(output) 
    print('##############################################################\n')

fd.close()
2
  • Bro, you're crazy late to the party.
    – D3l_Gato
    Sep 13, 2016 at 18:37
  • 1
    Yeah cant deny that but better late then never though right? Sep 26, 2016 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.