Pages

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.


1 comment:

  1. Ensure that the casino that you choose has all of the video games you like to play. There isn't any need to go to a specific casino simply because it has a good status tons of|and plenty of} video games in case you are a poker fanatic. For instance, if the rollover amount is 35x, this means {you need to|you should|you have to} guess a total of 35 occasions the amount of your initial deposit before find a way to|you possibly can} withdraw any winnings made off of your bonus cash. Besides their huge collection of video games, mBit additionally has one of the best introductory promotions around, providing 110% as much as} 1 BTC 코인카지노 and 300 free spins. Moreover, mBit has one of the quickest withdrawal occasions, as most withdrawals are completed in beneath 10 minutes. With the sheer number of options, each one providing one thing slightly different, it may be} onerous to make the best choice.

    ReplyDelete