Network Automation Scripts

Python Scripts for Automation in Network

Python is a Robust and popular programming language with enormous capabilities for automation. Network Automation capabilities can be further enhanced with the use of Python. Written down are some of the methods to enable the use of Python as a scripting language in network automation.

Install Python on the network automation server and, Install Paramiko using “pip” on the network automation server. Paramiko is an SSH library for Python.

Configure Python interpreter in network automation

As it needs to know the location of the interpreter before executing the script. Need to provide the location of the Python binary installed on the network automation server, also provide network automation UI. Steps are –

1. Login into Network Automation UI.

2.Navigate to the Server settings (Admin -> Administrative Settings -> Server).

3.Locate the Advanced Scripting section on the page.

4.Pick any of the empty scripting languages and provide the details.

Scripting Language – the name of the language as it needs to appear in the UI.
Path to Interpreter – location of the python binary. (on Linux – /usr/bin/python).

5.Save the changes.

Verify the configuration
The changes made in the Server settings will result in a new option while creating a new advanced Diagnostic/Change Plan.

1.Navigate to the first diagnostic page. (Devices->Device tools-> Diagnostic->New Diagnostic)

2.Select the Advanced scripting checkbox

3.Verify that the Language drop-down has the newly added scripting language.

Create advanced scripts using Python

Create and use Python scripts that can connect to devices and perform advanced data manipulation. These scripts can be developed in the Script area of both Change Plans and Diagnostics.

Convert SSH/Telnet sessions to the device via Network Automation Proxy to Python scripts

One of the powerful features of network automation is connecting to the device via network automation proxy and recording all the steps performed on that device. This recorded procedure can be converted to a script and executed as a Change Plan against a bunch of devices. This section will show the steps to convert the recorded sessions to a Python script, which can then be altered according to the needs and executed against a host of devices.

Configure to display “Convert to Python” menu in the View Session page

1.Open the file adjustable_options.rcx for editing (found in <NA install dir>/jre/folder)

2.Copy the xml content below and paste it after the last </option> tag and before </options> tag.

3.Save the file.

<!– pthon –>

<option name =”AutoScript/python “>true</option>

<option name=”AutoScript/python/header/default” notrim=”true”><![CDATA[

import base64

import paramiko

import sys

import time

def get_output(session):

output = ‘ ‘

data = ‘ ‘

time.sleep(1)

not_done = True

MAX_RETRY = 5

while (not_done or MAX_RETRY > 0):

if (session.recv_ready()):

data = session.recv(65535).decode(‘ascii’)

output += data

if MAX_RETRY < 5

MAX_RETRY = 5

else

not_done = False

MAX_RETRY = MAX_RETRY – 1

return output

client = paramiko.SSHClient()

client.load_system_host_keys()

device = ‘#$tc_device_id$’

sshport = ‘$tc_proxy_ssh_port$’

client.set_mission_host_key_policy(paramiko.AutoAddPolicy())

client.connect (‘ localhost’, username=’$tc_user_username$’, password=’$NA_password$’, port=sshport)

transport = client.get _transport()

session = transport.open_session()

session.set_combine_stderr(True)

session.invoke_shell()

get_output(session)

session.send( ‘ connect ‘ + device + ‘/n’)

output = get_output(session)

print(output)

session.send(‘terminal length 0/n’ )

output = get_output (session)

]]>

</option>

<option name=”AutoScript/python/percommand/default” notrim =”true”><![CDATA[

print(‘<<CMD>>\n)

session.send(‘<<CMD>>\n)

output = get_output (session)

print( ‘\n’ + output )

]]>

</option>

<option name =”AutoScript/python/footer/default” notrim=”true”><![CDATA[

session.end( ‘exit\n’ )

output = get_output(session)

print (output)

client.close ()

]]>

</option>

<option name=”AutoScript/python/logout/default”>exit,quit, logout,lo</option>

</option>

3.Save the file

4.Connect to network automation Proxy via SSH/Telnet (Ex: ssh <na_user>@<nahost>-p 8022)

5.Run the command -reload server options

6.Exit network automation proxy

Record an SSH session

1.Connect to NA proxy.

2.Connect to a device shell managed by NA using connect command.

3.Execute the device-specific commands.

4.Exit the device shell.

Convert recorded session to Python scripts

1.Navigate to the device home page (device for which session was recorded)

2. Navigate to the list of session page (View ->SSH/Telnet session)

3.Select any recorded session and click on View Command Only

4.Click on Convert to Python Script in the top right corner

5.On the new Change Plan page, modify the script if required

6.Click on the Pull Variables button

7.Mark the variable as Password by clicking on the checkbox

8.Provide a name to the change plan

9.Save the change plan

This change plan can be executed as a task against a set of devices of the same device family. While scheduling the Change Plan task, provide the password of the login in network automation user for the NA_Password variable.

Python Client to invoke NA SOAP APIs

Network automation exposes many APIs which can be invoked using SOAP (Simple Object Access Protocol). The APIs(operations) that can be invoked are documented in a WSDL which can be accessed via the network automation URL -http://<NA_Host>/soap?wsdl.

One can write Python scripts to invoke these APIs and automate the tasks that the user wishes to run. These scripts can be executed from any server which has python and the independent modules installed.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top

Subscribe

* indicates required