This repository contains a sampling of Python scripts that can be used to gather information from the network using different libraries and tools available to network automation engineers.
Many of these samples started as examples I created during video sessions with David Bombal.
Feel free to use any of the examples you find here as you wish, but I highly suggest testing them out in a lab environment (such as the DevNet Sandbox shown below) before using them in a production network.
If you've any ideas or requests for other samples, feel free to open up an Issue, but there is no SLA or guarantees provided :-)
These scripts and the examples assume you have already setup your laptop with the basic set of dev tools network automation engineers need. If you need some help there, checkout the DevNet Learning Labs on Development Environment Setup
Some of the Python libraries used in the examples require a Linux environment and won't run natively on Windows. If you are Windows user, you can still run these examples using one of the options here.
I actually have a "DevBox" Container that you can use easily with:
bash
docker run -it hpreston/devbox
* Use a Linux VM
* Many engineers I've talked with have setup a Linux VM on their Windows workstations for this specific need.
All of the scripts have been built and tested using the DevNet Multi-IOS Sandbox, a free development lab available for anyone to reserve and use. An easy way to try them yourself is to reserve a lab and follow along.
Note: If you have your own VIRL server available, you of course use it instead.
First, reserve a Sandbox and connect to its VPN (details provided in email or in the Sandbox Portal). When reserving, you can pick "None" as the VIRL Simulation as we'll be starting one up momentarily.
We are "network developers" so we'll be using our Python and API skills for these examples. Let's setup a Python3 virtual environment and install the great virlutils library to get our sample network working.
One method of creating a venv is shown here, feel free to use whatever method you prefer and works on your platform.
```bash python3.6 -m venv venv source venv/bin/activate pip install requests virlutils
```
Now we can start our network simulation. If you've your own topology file, feel free to use that. Otherwise let's start up one of the samples.
bash
virl up virlfiles/5_router_mesh
Monitor the status of the network start with virl
commands. For example:
```bash
virl nodes
virl console iosv-1
virl ssh iosv-1
virl --help ```
Some of these examples will leverage Ansible and/or pyATS/Genie within them. virlutils provides an easy way to generate inventory for both from your simulation.
```bash
virl generate ansible -o inventory.yaml
virl generate pyats -o testbed.yaml ```
Note: Ansible and pyATS/Genie examples require a Linux or macOS environment and won't work on Windows. See above for tips if you're a Windows user.
Checkout some of the demo's included.
Note: This repo and examples started on May 11th, 2019 and is a work in progress in case you're wondering why so few examples so far :-)
Let's create a CSV file containing all the MAC addresses for every interface in our network.
Note: This example leverages the Genie library and requires a macOS or Linux environment.
Libraries Used
Note: if you'd like to explore this example within an interactive Python environment, checkout
interface_report_interactive.py
for a walkthrough designed to be used in iPython or another interactive window.
Install the necessary Python libraries into your virtual environment.
bash
pip install genie virlutils
A requirements.txt
file is included in the repo with all the libraries used for all examples. You could pip install -r requirements.txt
instead.
csv
is included with Python standard library
If you haven't already started your network simulation in VIRL and created the testbed file, do that now.
bash
virl up --provision virlfiles/5_router_mesh
virl generate pyats -o testbed.yaml
Genie will need the network credentials to connect to the devices. The virlutils
generated testbed file assumes you will have set Environment Variables for PYATS_USERNAME
, PYATS_PASSWORD
and PYATS_AUTH_PASS
.
```yaml
testbed:
name: network_info
tacacs:
username: "%ENV{PYATS_USERNAME}"
passwords:
tacacs: "%ENV{PYATS_PASSWORD}"
enable: "%ENV{PYATS_AUTH_PASS}"
line: "%ENV{PYATS_PASSWORD}"
```
VIRL defaults to cisco
for all of these. This repository has a src_env
file included that will set up these environment variables. Just "source" it.
bash
source src_env
Run the script to generate the report.
python interface_report.py
Take a look at interfaces.csv
file to see the data. Here I'll use less
, but you can leverage Excel or any other program you wish.
```bash less interfaces.csv
Device,Interface,MAC Address iosv-1,GigabitEthernet0/0,5e00.8000.0000 iosv-1,GigabitEthernet0/1,fa16.3ea0.2e5e iosv-1,GigabitEthernet0/2,fa16.3e01.3226 iosv-1,GigabitEthernet0/3,fa16.3ecc.abc5 iosv-1,GigabitEthernet0/4,fa16.3ecd.dc7b iosv-1,Loopback0,N/A iosv-2,GigabitEthernet0/0,5e00.8001.0000 iosv-2,GigabitEthernet0/1,fa16.3e99.d270 iosv-2,GigabitEthernet0/2,fa16.3ee3.4fcf iosv-2,GigabitEthernet0/3,fa16.3e51.be1c iosv-2,GigabitEthernet0/4,fa16.3e05.6cd7 ```
Bumps certifi from 2019.3.9 to 2022.12.7.
9e9e840
2022.12.07b81bdb2
2022.09.24939a28f
2022.09.14aca828a
2022.06.15.2de0eae1
Only use importlib.resources's new files() / Traversable API on Python ≥3.11 ...b8eb5e9
2022.06.15.147fb7ab
Fix deprecation warning on Python 3.11 (#199)b0b48e0
fixes #198 -- update link in license9d514b4
2022.06.154151e88
Add py.typed to MANIFEST.in to package in sdist (#196)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps lxml from 4.3.3 to 4.9.1.
Sourced from lxml's changelog.
4.9.1 (2022-07-01)
Bugs fixed
- A crash was resolved when using
iterwalk()
(orcanonicalize()
) after parsing certain incorrect input. Note thatiterwalk()
can crash on valid input parsed with the same parser after failing to parse the incorrect input.4.9.0 (2022-06-01)
Bugs fixed
- GH#341: The mixin inheritance order in
lxml.html
was corrected. Patch by xmo-odoo.Other changes
Built with Cython 0.29.30 to adapt to changes in Python 3.11 and 3.12.
Wheels include zlib 1.2.12, libxml2 2.9.14 and libxslt 1.1.35 (libxml2 2.9.12+ and libxslt 1.1.34 on Windows).
GH#343: Windows-AArch64 build support in Visual Studio. Patch by Steve Dower.
4.8.0 (2022-02-17)
Features added
GH#337: Path-like objects are now supported throughout the API instead of just strings. Patch by Henning Janssen.
The
ElementMaker
now supportsQName
values as tags, which always override the default namespace of the factory.Bugs fixed
- GH#338: In lxml.objectify, the XSI float annotation "nan" and "inf" were spelled in lower case, whereas XML Schema datatypes define them as "NaN" and "INF" respectively.
... (truncated)
d01872c
Prevent parse failure in new test from leaking into later test runs.d65e632
Prepare release of lxml 4.9.1.86368e9
Fix a crash when incorrect parser input occurs together with usages of iterwa...50c2764
Delete unused Travis CI config and reference in docs (GH-345)8f0bf2d
Try to speed up the musllinux AArch64 build by splitting the different CPytho...b9f7074
Remove debug print from test.b224e0f
Try to install 'xz' in wheel builds, if available, since it's now needed to e...897ebfa
Update macOS deployment target version from 10.14 to 10.15 since 10.14 starts...853c9e9
Prepare release of 4.9.0.d3f77e6
Add a test for https://bugs.launchpad.net/lxml/+bug/1965070 leaving out the a...Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps ipython from 7.5.0 to 7.16.3.
d43c7c7
release 7.16.35fa1e40
Merge pull request from GHSA-pq7m-3gw7-gq5x8df8971
back to dev9f477b7
release 7.16.2138f266
bring back release helper from master branch5aa3634
Merge pull request #13341 from meeseeksmachine/auto-backport-of-pr-13335-on-7...bcae8e0
Backport PR #13335: What's new 7.16.28fcdcd3
Pin Jedi to <0.17.2.2486838
release 7.16.120bdc6f
fix conda buildDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps urllib3 from 1.24.3 to 1.26.5.
Sourced from urllib3's releases.
1.26.5
:warning: IMPORTANT: urllib3 v2.0 will drop support for Python 2: Read more in the v2.0 Roadmap
- Fixed deprecation warnings emitted in Python 3.10.
- Updated vendored
six
library to 1.16.0.- Improved performance of URL parser when splitting the authority component.
If you or your organization rely on urllib3 consider supporting us via GitHub Sponsors
1.26.4
:warning: IMPORTANT: urllib3 v2.0 will drop support for Python 2: Read more in the v2.0 Roadmap
- Changed behavior of the default
SSLContext
when connecting to HTTPS proxy during HTTPS requests. The defaultSSLContext
now setscheck_hostname=True
.If you or your organization rely on urllib3 consider supporting us via GitHub Sponsors
1.26.3
:warning: IMPORTANT: urllib3 v2.0 will drop support for Python 2: Read more in the v2.0 Roadmap
Fixed bytes and string comparison issue with headers (Pull #2141)
Changed
ProxySchemeUnknown
error message to be more actionable if the user supplies a proxy URL without a scheme (Pull #2107)If you or your organization rely on urllib3 consider supporting us via GitHub Sponsors
1.26.2
:warning: IMPORTANT: urllib3 v2.0 will drop support for Python 2: Read more in the v2.0 Roadmap
- Fixed an issue where
wrap_socket
andCERT_REQUIRED
wouldn't be imported properly on Python 2.7.8 and earlier (Pull #2052)1.26.1
:warning: IMPORTANT: urllib3 v2.0 will drop support for Python 2: Read more in the v2.0 Roadmap
- Fixed an issue where two
User-Agent
headers would be sent if aUser-Agent
header key is passed asbytes
(Pull #2047)1.26.0
:warning: IMPORTANT: urllib3 v2.0 will drop support for Python 2: Read more in the v2.0 Roadmap
Added support for HTTPS proxies contacting HTTPS servers (Pull #1923, Pull #1806)
Deprecated negotiating TLSv1 and TLSv1.1 by default. Users that still wish to use TLS earlier than 1.2 without a deprecation warning should opt-in explicitly by setting
ssl_version=ssl.PROTOCOL_TLSv1_1
(Pull #2002) Starting in urllib3 v2.0: Connections that receive aDeprecationWarning
will failDeprecated
Retry
optionsRetry.DEFAULT_METHOD_WHITELIST
,Retry.DEFAULT_REDIRECT_HEADERS_BLACKLIST
andRetry(method_whitelist=...)
in favor ofRetry.DEFAULT_ALLOWED_METHODS
,Retry.DEFAULT_REMOVE_HEADERS_ON_REDIRECT
, andRetry(allowed_methods=...)
(Pull #2000) Starting in urllib3 v2.0: Deprecated options will be removed
... (truncated)
Sourced from urllib3's changelog.
1.26.5 (2021-05-26)
- Fixed deprecation warnings emitted in Python 3.10.
- Updated vendored
six
library to 1.16.0.- Improved performance of URL parser when splitting the authority component.
1.26.4 (2021-03-15)
- Changed behavior of the default
SSLContext
when connecting to HTTPS proxy during HTTPS requests. The defaultSSLContext
now setscheck_hostname=True
.1.26.3 (2021-01-26)
Fixed bytes and string comparison issue with headers (Pull #2141)
Changed
ProxySchemeUnknown
error message to be more actionable if the user supplies a proxy URL without a scheme. (Pull #2107)1.26.2 (2020-11-12)
- Fixed an issue where
wrap_socket
andCERT_REQUIRED
wouldn't be imported properly on Python 2.7.8 and earlier (Pull #2052)1.26.1 (2020-11-11)
- Fixed an issue where two
User-Agent
headers would be sent if aUser-Agent
header key is passed asbytes
(Pull #2047)1.26.0 (2020-11-10)
NOTE: urllib3 v2.0 will drop support for Python 2.
Read more in the v2.0 Roadmap <https://urllib3.readthedocs.io/en/latest/v2-roadmap.html>
_.Added support for HTTPS proxies contacting HTTPS servers (Pull #1923, Pull #1806)
Deprecated negotiating TLSv1 and TLSv1.1 by default. Users that still wish to use TLS earlier than 1.2 without a deprecation warning
... (truncated)
d161647
Release 1.26.52d4a3fe
Improve performance of sub-authority splitting in URL2698537
Update vendored six to 1.16.007bed79
Fix deprecation warnings for Python 3.10 ssl moduled725a9b
Add Python 3.10 to GitHub Actions339ad34
Use pytest==6.2.4 on Python 3.10+f271c9c
Apply latest Black formatting1884878
[1.26] Properly proxy EOF on the SSLTransport test suitea891304
Release 1.26.48d65ea1
Merge pull request from GHSA-5phf-pp7p-vc2rDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps pygments from 2.4.0 to 2.7.4.
Sourced from pygments's releases.
2.7.4
Updated lexers:
Fix infinite loop in SML lexer (#1625)
Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)
Limit recursion with nesting Ruby heredocs (#1638)
Fix a few inefficient regexes for guessing lexers
Fix the raw token lexer handling of Unicode (#1616)
Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!
Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)
Fix incorrect MATLAB example (#1582)
Thanks to Google's OSS-Fuzz project for finding many of these bugs.
2.7.3
... (truncated)
Sourced from pygments's changelog.
Version 2.7.4
(released January 12, 2021)
Updated lexers:
Fix infinite loop in SML lexer (#1625)
Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)
Limit recursion with nesting Ruby heredocs (#1638)
Fix a few inefficient regexes for guessing lexers
Fix the raw token lexer handling of Unicode (#1616)
Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!
Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)
Fix incorrect MATLAB example (#1582)
Thanks to Google's OSS-Fuzz project for finding many of these bugs.
Version 2.7.3
(released December 6, 2020)
... (truncated)
4d555d0
Bump version to 2.7.4.fc3b05d
Update CHANGES.ad21935
Revert "Added dracula theme style (#1636)"e411506
Prepare for 2.7.4 release.275e34d
doc: remove Perl 6 ref2e7e8c4
Fix several exponential/cubic complexity regexes found by Ben Caller/Doyenseceb39c43
xquery: fix pop from empty stack2738778
fix coding style in test_analyzer_lexer02e0f09
Added 'ERROR STOP' to fortran.py keywords. (#1665)c83fe48
support added for css variables (#1633)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps pyyaml from 5.1 to 5.4.
Sourced from pyyaml's changelog.
5.4 (2021-01-19)
- yaml/pyyaml#407 -- Build modernization, remove distutils, fix metadata, build wheels, CI to GHA
- yaml/pyyaml#472 -- Fix for CVE-2020-14343, moves arbitrary python tags to UnsafeLoader
- yaml/pyyaml#441 -- Fix memory leak in implicit resolver setup
- yaml/pyyaml#392 -- Fix py2 copy support for timezone objects
- yaml/pyyaml#378 -- Fix compatibility with Jython
5.3.1 (2020-03-18)
- yaml/pyyaml#386 -- Prevents arbitrary code execution during python/object/new constructor
5.3 (2020-01-06)
- yaml/pyyaml#290 -- Use
is
instead of equality for comparing withNone
- yaml/pyyaml#270 -- Fix typos and stylistic nit
- yaml/pyyaml#309 -- Fix up small typo
- yaml/pyyaml#161 -- Fix handling of slots
- yaml/pyyaml#358 -- Allow calling add_multi_constructor with None
- yaml/pyyaml#285 -- Add use of safe_load() function in README
- yaml/pyyaml#351 -- Fix reader for Unicode code points over 0xFFFF
- yaml/pyyaml#360 -- Enable certain unicode tests when maxunicode not > 0xffff
- yaml/pyyaml#359 -- Use full_load in yaml-highlight example
- yaml/pyyaml#244 -- Document that PyYAML is implemented with Cython
- yaml/pyyaml#329 -- Fix for Python 3.10
- yaml/pyyaml#310 -- Increase size of index, line, and column fields
- yaml/pyyaml#260 -- Remove some unused imports
- yaml/pyyaml#163 -- Create timezone-aware datetimes when parsed as such
- yaml/pyyaml#363 -- Add tests for timezone
5.2 (2019-12-02)
- Repair incompatibilities introduced with 5.1. The default Loader was changed, but several methods like add_constructor still used the old default yaml/pyyaml#279 -- A more flexible fix for custom tag constructors yaml/pyyaml#287 -- Change default loader for yaml.add_constructor yaml/pyyaml#305 -- Change default loader for add_implicit_resolver, add_path_resolver
- Make FullLoader safer by removing python/object/apply from the default FullLoader yaml/pyyaml#347 -- Move constructor for object/apply to UnsafeConstructor
- Fix bug introduced in 5.1 where quoting went wrong on systems with sys.maxunicode <= 0xffff yaml/pyyaml#276 -- Fix logic for quoting special characters
- Other PRs: yaml/pyyaml#280 -- Update CHANGES for 5.1
5.1.2 (2019-07-30)
- Re-release of 5.1 with regenerated Cython sources to build properly for Python 3.8b2+
... (truncated)
58d0cb7
5.4 releasea60f7a1
Fix compatibility with Jythonee98abd
Run CI on PR base branch changesddf2033
constructor.timezone: _copy & deepcopyfc914d5
Avoid repeatedly appending to yaml_implicit_resolversa001f27
Fix for CVE-2020-14343fe15062
Add 3.9 to appveyor file for completeness sake1e1c7fb
Add a newline character to end of pyproject.toml0b6b7d6
Start sentences and phrases for capital lettersc976915
Shell code improvementsDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.