-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
620 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
|
||
# https://github.com/github/gitignore/blob/master/Python.gitignore | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# devnet-experiments | ||
Scripts experimenting with elements of devnet coding | ||
|
||
This repository contains scripts which I am using to test out logic or functions related to devnet type topics. | ||
|
||
The scripts are by nature very hacky, so don't @ me about it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Bootstrap | ||
|
||
Bringing together the results from templating and threading, as well as the netmiko example from the VIRL/CML dicumentation, this script configures up 5 switches in a VIRL environment to give them hostnames, management IPs and enable ssh. | ||
|
||
One thing I have noticed is that netmiko doesn't seem to handle console messages very well when connected to a device console, so that gets disabled before the script applies any true configuration. | ||
|
||
It largely works, but sometimes the netmiko redispatch fails. I haven't worked out why that is. It may be that the default hostname on the IOSv_L2 images in VIRL has an underscore in it and that throws the prompt parser. | ||
|
||
Regardless, the script appears to work on a second run, so it's not a major issue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import yaml | ||
import netmiko | ||
import time | ||
import jinja2 | ||
import getpass | ||
from concurrent.futures import ThreadPoolExecutor | ||
from threading import Lock | ||
from virl2_client import ClientLibrary | ||
|
||
DEFAULTS = { | ||
"domainname":"lab.home", | ||
"mgmt_subnet":"255.255.255.0", | ||
"mgmt_vrf":"mgmt-vrf", | ||
"mgmt_vlan":"10", | ||
"default_gw":"10.144.35.1", | ||
"dev_username":"cisco", | ||
"dev_password":"cisco", | ||
"lab_id":"Lab1" | ||
} | ||
|
||
print_lock = Lock() | ||
|
||
def config_worker(param): | ||
|
||
netmiko_delay=0.1 | ||
|
||
client = ClientLibrary(param["virl_controller"], | ||
param["virl_username"], | ||
param["virl_password"], | ||
ssl_verify=False) | ||
client.wait_for_lld_connected() | ||
|
||
with print_lock: | ||
print(f"{param['hostname']}: Connected to VIRL") | ||
|
||
our_lab = client.find_labs_by_title(param["lab_id"])[0] | ||
our_node = our_lab.get_node_by_label(param["hostname"]) | ||
|
||
|
||
with print_lock: | ||
print(f"{param['hostname']}: Identified lab and device: /{our_lab.id}/{our_node.id}/0") | ||
|
||
c = netmiko.ConnectHandler(device_type='terminal_server', | ||
host=param["virl_controller"], | ||
username=param["virl_username"], | ||
password=param["virl_password"]) | ||
|
||
with print_lock: | ||
print(f"{param['hostname']}: Connected to terminal server") | ||
|
||
c.write_channel('\r\n') | ||
time.sleep(1) | ||
c.write_channel('\r\n') | ||
time.sleep(1) | ||
|
||
c.write_channel(f'open /{our_lab.id}/{our_node.id}/0\r') | ||
|
||
c.write_channel('\r\n') | ||
time.sleep(1) | ||
c.write_channel('\r\n') | ||
time.sleep(1) | ||
c.write_channel('\r\n') | ||
time.sleep(1) | ||
c.write_channel('\r\n') | ||
|
||
with print_lock: | ||
print(f"{param['hostname']} : Switching to IOS interpreter") | ||
try: | ||
netmiko.redispatch(c, device_type='cisco_ios') | ||
except Exception as e: | ||
with print_lock: | ||
print(f"{param['hostname']} : Failed to switch to IOS interpreter. {e}") | ||
c.find_prompt() | ||
#c.enable() | ||
|
||
with print_lock: | ||
print(f"{param['hostname']} : Preparing config") | ||
try: | ||
with open('swtemplate.j2','r') as f: | ||
template = jinja2.Template(f.read()) | ||
config_to_send = template.render(device=param).split('\n') | ||
except: | ||
with print_lock: | ||
print(f"{param['hostname']} : Failed to prepare config") | ||
|
||
|
||
with print_lock: | ||
print(f"{param['hostname']} : Sending config") | ||
|
||
try: | ||
## I have found that console messages can upset netmiko, so turn them off with a timed command | ||
c.send_command_timing(f"enable", netmiko_delay) | ||
c.send_command_timing(f"conf t", netmiko_delay) | ||
c.send_command_timing(f"no logging console", netmiko_delay) | ||
c.send_command_timing("end", netmiko_delay) | ||
time.sleep(1) | ||
c.write_channel('\r\n') | ||
c.find_prompt() | ||
c.send_config_set(config_to_send) | ||
with print_lock: | ||
print(f"{param['hostname']} : Config sent") | ||
|
||
except Exception as e: | ||
with print_lock: | ||
print(f"{param['hostname']} : Error, send config failed. {e}") | ||
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
with open('devices.yaml','r') as f: | ||
devices = yaml.safe_load(f) | ||
|
||
DEFAULTS["virl_controller"] = input("VIRL controller: ") | ||
DEFAULTS["virl_username"] = input('VIRL username: ') | ||
DEFAULTS["virl_password"] = getpass.getpass('VIRL password: ') | ||
|
||
with ThreadPoolExecutor(max_workers=5) as executor: | ||
for device in devices: | ||
executor.submit(config_worker, ({**DEFAULTS, **device})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
- hostname: "S1" | ||
mgmt_ip: "10.144.35.21" | ||
- hostname: "S2" | ||
mgmt_ip: "10.144.35.22" | ||
- hostname: "S3" | ||
mgmt_ip: "10.144.35.23" | ||
- hostname: "S4" | ||
mgmt_ip: "10.144.35.24" | ||
- hostname: "S5" | ||
mgmt_ip: "10.144.35.25" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
hostname {{ device.hostname }} | ||
no ip domain-lookup | ||
ip domain-name {{ device.domainname }} | ||
! | ||
vlan {{ device.mgmt_vlan }} | ||
name mgmt_vlan | ||
exit | ||
! | ||
vrf definition mgmt-vrf | ||
address-family ipv4 | ||
exit-address-family | ||
! | ||
interface vlan {{ device.mgmt_vlan }} | ||
vrf forwarding {{ device.mgmt_vrf }} | ||
ip address {{ device.mgmt_ip }} {{ device.mgmt_subnet }} | ||
no shut | ||
! | ||
int gi0/0 | ||
swi mod acc | ||
swi acc vla {{ device.mgmt_vlan }} | ||
descr Management | ||
no shut | ||
! | ||
int ra gi0/1 - 3 | ||
shut | ||
! | ||
ip route vrf {{ device.mgmt_vrf }} 0.0.0.0 0.0.0.0 {{ device.default_gw }} | ||
! | ||
username cisco priv 15 password cisco | ||
! | ||
line vty 0 16 | ||
login local | ||
transport input all | ||
! | ||
no banner incoming | ||
no banner exec | ||
no banner login | ||
! | ||
crypto key generate rsa modulus 1024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Playing with Jinja2 | ||
|
||
Just experimenting with using jinja2 for templating switch configuration, and also using yaml to bring in the inventory and device configs. | ||
|
||
This script doesn't connect to anything to make any changes, it just prints out what it might send. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
- hostname: "S1" | ||
mgmt_if: "vlan1" | ||
mgmt_ip: "10.144.35.21" | ||
interfaces: | ||
- name: "Gi0/0" | ||
description: "Connection to mgmt switch" | ||
active: True | ||
mode: "access" | ||
vlans: "1" | ||
- name: "Gi0/1" | ||
description: "N/C" | ||
active: False | ||
- name: "Gi0/2" | ||
description: "N/C" | ||
active: False | ||
- name: "Gi0/3" | ||
description: "N/C" | ||
active: False | ||
# - hostname: "S2" | ||
# mgmt_if: "vlan1" | ||
# mgmt_ip: "10.144.35.22" |
Oops, something went wrong.