http://api.extremenetworks.com/
This script can be used to apply different configuration to groups of switch types:
from netmiko import ConnectHandler
s1 = {
‘device_type’: ‘avaya_vsp’,
‘ip’: ‘192.168.1.1’,
‘username’: ‘rwa’,
‘password’: ‘rwa’,
}
with open(‘core_config.txt’) as f:
lines = f.read().splitlines()
print(lines)
all_devices = [s1]
for devices in all_devices:
net_connect = ConnectHandler(**devices)
print(net_connect.find_prompt())
net_connect.enable()
print(net_connect.find_prompt())
net_connect.config_mode()
net_connect.send_config_set(lines)
print(net_connect.find_prompt())
net_connect.disconnect()
Enter username and password to use with Telnet. Loop for range of IP addresses and inside loop again and create new VLAN and name the VLAN. This script connects to two switches using Telnet and adds new configuration.
#!usr/bin/env python
import getpass
import sys
import telnetlib
import time
user = raw_input(“Enter your telnet username: “)
password = getpass.getpass()
print (user, password)
for i in range (1,3):
HOST = ‘192.168.1.’ + str(i)
print (HOST)
tn = telnetlib.Telnet(HOST)
tn.read_until(‘Login: ‘)
tn.write(user + ‘\n’)
if password:
tn.read_until(‘Password: ‘)
tn.write(password + ‘\n’)
time.sleep(5)
tn.write(‘enable’ + ‘\n’)
tn.write(‘conf t’ + ‘\n’)
for v in range (400,404):
print (‘vlan create ‘ + str(v) + ‘ type port-mstprstp 0’ + ‘\n’)
tn.write(‘vlan create ‘ + str(v) + ‘ type port-mstprstp 0’ + ‘\n’)
time.sleep(2)
print (‘vlan name ‘ + str(v) + ‘ VLAN_’ +str(v) + ‘\n’)
tn.write(‘vlan name ‘ + str(v) + ‘ VLAN_’ +str(v) + ‘\n’)
tn.write(‘end’ + ‘\n’)
tn.write(‘exit’ + ‘\n’)
print (“Good bye!”)
Using a list of switch IP addresses, prompt for username and password and capture troubleshooting information for each switch:
import paramiko
import time
import getpass
username = raw_input(‘Enter your username: ‘)
password = getpass.getpass()
f = open (‘myswitches.txt’)
for line in f:
ip_address = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print ‘Successful connection’, ip_address
remote_connection = ssh_client.invoke_shell()
print ‘Collecting troubeshooting log file of ‘ + ip_address
remote_connection.send(‘show clock\n’)
remote_connection.send(‘enable\n’)
remote_connection.send(‘terminal more disable\n’)
remote_connection.send(‘show run\n’)
remote_connection.send(‘terminal more enable\n’)
time.sleep(20)
readoutput = remote_connection.recv(655350)
saveoutput = open(‘Log file of ‘ + ip_address, ‘w’)
print ‘Saving to file called Log file of ‘ + ip_address + ‘\n’
saveoutput.write(readoutput)
saveoutput.write(‘\n’)
saveoutput.close
ssh_client.close()
Shell output..
$ python sshclient.py
Enter your username: rwa
Password:
Successful connection 192.168.1.1
Collecting troubeshooting log file of 192.168.1.1
Saving to file called Log file of 192.168.1.1
Successful connection 192.168.1.2
Collecting troubeshooting log file of 192.168.1.2
Saving to file called Log file of 192.168.1.2
Sample of Log file of 192.168.1.1…
core1:1>enable
core1:1#terminal more disable
core1:1#show run
Preparing to Display Configuration…
#
# Fri Apr 20 18:40:19 2018 UTC
# box type : VSP-8284XSQ
# software version : 7.0.0.0_B885
# cli mode : ECLI
#
Content of myswitches.txt:
192.168.1.1
192.168.1.2
Script which will telnet to a switch and create a new VLAN:
import getpass
import sys
import telnetlib
HOST = ‘192.168.1.1’
user = raw_input(‘Enter your telnet username: ‘)
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(‘Login: ‘)
tn.write(user + ‘\n’)
if password:
tn.read_until(‘Password: ‘)
tn.write(password + ‘\n’)
tn.write(‘enable\n’)
tn.write(‘conf t\n’)
tn.write(‘vlan create 101 type port-mstprstp 0\n’)
tn.write(‘exit\n’)
tn.write(‘exit\n’)
Login to a switch with SSHv2 and run a command ‘show clock’:
Paramiko SSHv2 Login
>>> import paramiko
>>> import time
>>> ip_address = ‘192.168.1.1’
>>> username = ‘rwa’
>>> password = ‘rwa’
>>> ssh_client = paramiko.SSHClient()
>>> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh_client.connect(hostname=ip_address,username=username,password=password)
>>> print “Successful connection”, ip_address
Successful connection 192.168.1.1
>>> remote_connection = ssh_client.invoke_shell()
>>> remote_connection.send(“show clock\n”)
11
>>> output = remote_connection.recv(65535)
>>> print output>>> ssh_client.close()
EXTREME NETWORKS VOSS COMMAND LINE INTERFACE
core1:1>show clock
Fri Apr 20 11:24:39 2018 UTC
Note: This could be adapted to SSH to a remote switch and capture the running configuration and store in a filename by redirecting the output of the python script to a local file eg python showrun.py > showrun.cfg.
remote_connection.send(‘terminal more disable\n’)
remote_connection.send(‘enable\n’)
remote_connection.send(‘show run\n’)
remote_connection.send(‘terminal more enable\n’)
Found problems with capturing the output to a file so I changed the code and works better…
import paramiko
import time
import sys
ip_address = ‘192.168.1.1’
username = ‘rwa’
password = ‘rwa’
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
remote_connection = ssh_client.invoke_shell()
remote_connection.send(‘show clock\n’)
remote_connection.send(‘enable\n’)
remote_connection.send(‘terminal more disable\n’)
remote_connection.send(‘show run\n’)
remote_connection.send(‘terminal more enable\n’)
time.sleep(20)
readoutput = remote_connection.recv(655350)
saveoutput = open(‘Log file’, ‘w’)
print ‘Saving to file called “Log file”…’
saveoutput.write(readoutput)
saveoutput.write(‘\n’)
saveoutput.close
ssh_client.close()
Create a .yml data file (data.yml) with variables and lists:
—
host_ports:
– 1/1
– 1/2
– 1/3
Create a .j2 file (noshut.j2) which will reference the data from the data file:
{%- for iface in host_ports %}
interface gigabitethernet {{ iface }}
no shutdown
exit
{%- endfor %}
Merge data and template together and output to a file:
python render.py > output.conf
Render.py:
from jinja2 import Template
import yaml
import sys
from glob import glob
data_fname = sys.argv[1:1] or glob(‘*.yml’)[0]
template_fname = sys.argv[2:2] or glob(‘*.j2’)[0]
data = yaml.load(open(data_fname).read())
template = Template(open(template_fname).read())
print template.render(data)
Adding data to a variable called myvars (native python structure aka dictionary):
import yaml
myvars = yaml.load(open(‘data.yml’).read())
from pprint import pprint
pprint(myvars)
{‘host_ports’: [‘1/1’, ‘1/2’, ‘1/3’]}
https://neckercube.com/index.php/2018/04/
Template (filename port_vlan.j2):
interface vlan {{ vlan_name }}
ip address {{ ip }} {{ mask }}
exit
Python code to build configuration which adds an IP address to a VLAN…
python
import jinja2
import os
loader = jinja2.FileSystemLoader(os.getcwd())
loader
jenv = jinja2.Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
template = jenv.get_template(‘port_vlan.j2′)
template
print (template.render(vlan_name=10, ip=’10.10.10.1′, mask=’255.255.255.0’))
Output:
interface vlan 10
ip address 10.10.10.1 255.255.255.0
exit
Create an Excel workbook with Cygwin:
Install PIP in console if not present…
easy_install-a.b pip # replace a.b with version of python
Install openpyxl…
pip3 install openpyxl
Create a workbook in Python3…
>>> from openpyxl import Workbook
>>> wb = Workbook()>>> ws = wb.active
Saving to a file…
>>> wb = Workbook()
>>> wb.save(‘balances.xlsx’)
http://openpyxl.readthedocs.io/en/stable/usage.html