Administrator Guide

17 Python Scripting for Dell Networking N-Series | Version 1.0.1
3.3 Example 3: Running Python scripts on an active switch
This example shows how to configure a Python script to be executed by the administrator at any time without
reloading the switch (unless the script itself is programmed to execute a reload). This particular script
performs the configuration tasks of creating and then verifying the creation of 1000 VLANs.
This example demonstrates the following areas:
i. Running a script on an active switch in production (without reloading)
ii. Using logic in scripts (e.g. loops, parsing, if-else)
iii. Modifying switch configurations (e.g. creating VLANs) using Python scripts
Save the commands below into a file using a script editor, then compress into a .tgz or .tar.gz format (as
discussed on page 8
).
Note: There is a limit of 16 characters for a script filename, including the extension.
#!/usr/bin/env python
#script creates 1000 VLANS (2-1002), then verifies 1000 were created
import sys, telnetlib, re
#variable declaration
#Replace the “xx.xx.xx.xx” below with the local host’s IP address
hostname = 'xx.xx.xx.xx'
username = 'admin'
password = 'password'
enPrompt = '>'
confPrompt = '#'
#open a telnet session to the device
print ("Opening telnet session to the device")
t = telnetlib.Telnet(hostname)
expect = t.read_until
send = t.write
expect('User:')
send(username + '\r')
expect('Password:')
send(password + '\r')
expect(enPrompt)
send('enable\r')
expect(confPrompt)
send('terminal length 0\r')
expect(confPrompt)
send('configure terminal\r')
expect(confPrompt)
#loop to create vlans