2013/10/09

Automation scripts to control Cisco devices

As a network engineer in complex network you can have many tasks that can easily be automated to simplify your network operation and reduce operation expenses. To help you automating network over telnet or ssh you can use Exscript (https://github.com/knipknap/exscript). It is a great tool, which simplify command execution over network with telnet or ssh. It could be used as command line tool or as python module. Since I want to show how easily you can establish connection to the remote device and execute commands and process this commands I will show you an example of the basic python script.

First you need to install Exscript to your system It works on python 2.6 or later. Exscript uses paramiko python module, which is used for ssh connections establishment. You can download Exscript via git or manually. I used manual method. And if you use manual method, just go in the directory where you extracted your Exscript and use python setup.py install. If installation go through without errors you are ready to use Exscript.

I want to show example how to get running config from your ASA device. Bellow you will find a simple script that connect to ASA device and get running config and save this running config to variable, which can be further processed based on your requirements.


# Exscript modules import to allow ssh 
# connection and login

from Exscript.protocols import SSH2
from Exscript import Account


# Connection establishment phase

account = Account('admin', 'cisco')
conn = SSH2()
conn.connect('192.168.1.1')
conn.login(account)


# Now you can execute commands in the 
# established ssh connection.
#
# Execute method accept string which is 
# send to remote device and new line command 
# is executed on remote device as well
#
# When you enter enable in your ASA, it waits 
# for the password. I have blank password so 
# I needed \n at the end of the command. 
# Exscript will actually send  enable\n\n.
#
# We also need to set pager to 0, so that 
# all command output is displayed at once 
# without <---  More  --->. Execute method 
# works in such way that command is send, 
# and then execute method is waiting for the 
# prompt. If <---  More  ---> is displayed, 
# prompt is never displayed and python return 
# traceback with the error.
#
# You are then ready to execute show running 
# command and save it to variable with 
# response method.

conn.execute('enable\n')
conn.execute('configure terminal')
conn.execute('pager 0')
conn.execute('show running')
running_config = conn.response

# Now you need to exit your ssh connection. Send 
# method is used because execute is waiting for 
# prompt, while send method only send command 
# without waiting for prompt, which will never 
# be displayed after exit command.
conn.execute('end')
conn.send('exit\r')

This was just a simple example to show how to connect to remote device via ssh and execute some commands. Command output could be further processed. Of course you can also execute some configuration commands. If you understand the script above I belive it is pretty straightforward to create some config scripts as well.


No comments:

Post a Comment