Exscript control cisco execute 'reload' command

2.6k views Asked by At

I'm ultimately trying to create some basic functions to control my cisco test lab devices using Exscript. Exscript has done everything perfectly for me up to this point and I'm just pulling a few functions for example.

What I'm having trouble with is creating this reload_start() function that executes the reload command, rebooting my Cisco device and reverting any changes I've made in my test run. I've tried dozens of different combinations of character strings to execute but cannot get it to work around the additional prompts that come up when typing 'reload'

In contrast, my copy_to_running_config() function works just fine, simply by adding '\n' at the end of the string.

I've not yet jumped into Exscript's prompt functions(get_prompt(), expect_prompt(), waitfor(), etc), and I'm guessing this is the avenue I need to explore, but I cannot find examples of this that deal with my specific goal.

from Exscript import Account
from Exscript.protocols import Telnet
from Exscript.protocols.drivers import ios

def __init__(self, ip):

    self.ip = ip
    self.account = Account('admin', 'key')              
    self.conn = Telnet()        
    self.conn.set_driver('ios')     
    self.conn.connect(ip)
    self.conn.login(self.account) 
    self.conn.execute('terminal length 0')

def enable(self):
    self.conn.execute('enable')

def copy_to_running_config(self, config):
    self.enable()
    self.conn.execute('copy flash:{0} running-config  \n'.format(config))
    res = self.conn.response
    self.conn.execute('exit')
    return res

def reload_start(self):
    self.enable()
    self.conn.execute('reload \n no \n')
    print self.conn.response

Any help or input would be greatly appreciated!

1

There are 1 answers

1
TobiasF On BEST ANSWER

Your Function reload_start should look like this:

def reload_start(self):
    self.enable()
    self.conn.set_prompt(r'Save\? \[yes/no\]\:')
    self.conn.execute('reload')
    self.conn.set_prompt(r'Proceed with reload\? \[confirm\]')
    self.conn.execute('no')
    self.conn.set_prompt()
    self.conn.execute('confirm')
    print self.conn.response

You have to set the Regular Expression for the prompt before executing the command. Otherwise Exscrpt is not be able to determine when to send the next command.

Nevertheless, if the configuration wasn't changed and the router doesn't ask you to save, the script above would not work because it would wait for the save-question.