Giter VIP home page Giter VIP logo

python_code_samples_network's Introduction

Network Automation with Python Code Samples

A collection of Python Code Samples for Network Management. Includes samples that leverage on-box libraries, as well as samples that use exposed external APIs (NETCONF/RESTCONF, SNMP, SSH, REST, etc). Some examples make use of available SDKs.

On-Box Examples

Many Cisco switches and routers provide an on-box Python Interpreter that can be leveraged to execute scripts and programs directly on end devices. In addition to the interpreter, Python libraries are included that provide direct access to the underlying devices operations to execute CLI commands, or monitor for events.

Sample Code

Code Sample Description
Execute CLI via Python This example is about as simple as it gets. By leveraging the CLI library, we execute the “show version” command on the box.
TDR Test Every Interface This example once again leverages the CLI library, but to do something a bit more interesting. A TDR test is run on every interface in “up” status.
EEM Config Changes to Spark In this example, the EEM library is used to monitor for configuration changes. When one occurs a message is sent to a Cisco Spark Room.
Python with Eventing Example Use the EEM and Python together to script based on local events.
EEM + Python + Spark ChatOps Use the EEM to monitor for config changes and send a Spark Message
EEM + Python + Email alert This example leverages the CLI library and using the EEM feature to monitor for interface flapping and send an email alert

Off-Box Examples

Here are few Python scripts that can interact with network elements using one of the many exposed interfaces (NETCONF, RESTCONF, SNMP, SSH, etc). Many of these scripts could also be run on-box, however they don’t leverage any of the unique libraries available on device.

Code Sample Description
Netmiko and CLI Example for Interface Management These are a series of python scripts for retrieving, creating, deleting a Loopback Interface with Python.
MIB Walk with Python In this example, we perform a MIB walk against a device leveraging the “netsnmp” library for Python.
NETCONF Connection with Python This example shows the basics of connecting to a device with NETCONF using the “ncclient” library for Python.
Configure Interface IP Address with RESTCONF In this example the newly ratified RESTCONF standard is used to configure the IP Address on an interface.
Get Inventory from APIC-EM APIC-EM maintains an inventory database of the entire network. In this example Python is used to retrieve that information using the REST API.
Get Host List from APIC-EM APIC-EM maintains a list of all clients connected to the network devices discovered by APIC-EM. This example queries the APIC-EM for the list, and display’s it in a simple table.
Retrieve Tenants from ACI APIC This example leverages the ACI Toolkit to connect to an APIC controller and retrieve the list of Tenants configured.
Basic NETCONF Get A basic ncclient example to <get> NETCONF Data
Basic NETCONF Edit A basic ncclient example to <edit-config> NETCONF Data
NETCONF XPATH Example Use the XPATH feature when making a NETCONF Requests
Model Based AAA These example scripts are for Model Based AAA to get, edit and delete the rule-lists for privilege level users and Groups by using ietf-netconf-acm.yang data model
RESTCONF These example scripts are for RESTCONF to retrieve and configure the switch using different operations such as Get, Delete, Put, Post and Patch.

python_code_samples_network's People

Contributors

alfred-leech avatar eckeltest avatar gregbo avatar hpreston avatar jafrazie avatar jclarke-csco avatar krishna426426 avatar pamosima avatar wendellodom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python_code_samples_network's Issues

updateip.py URL variables

I tried to connect to ios-xe-mgmt.cisco.com at port 9443 with nc but got no response. However, I was able to connect to port 443. So, I sshed to ios-xe-mgmt.cisco.com and was able to verify that it was configured to accept https requests, at least. This sandbox's configuration reminded me of another sandbox that I have recently used, sandbox-iosxe-recomm-1.cisco.com, which is actually a CNAME for it:

$ dig ios-xe-mgmt.cisco.com

; <<>> DiG 9.10.6 <<>> ios-xe-mgmt.cisco.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18601
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;ios-xe-mgmt.cisco.com. IN A

;; ANSWER SECTION:
ios-xe-mgmt.cisco.com. 2252 IN CNAME sandbox-iosxe-recomm-1.cisco.com.
sandbox-iosxe-recomm-1.cisco.com. 2252 IN A 131.226.217.149

At the very least, the port needs to be changed from 9443 to 443. But perhaps the script should be changed to allow the user to specify an alternate host, port, etc. in case those parameters need to be changed to access the sandbox associated with it.

NameError: name 'args' is not defined

When executed from idle(python v 3.6.4)
========================= RESTART: /root/updateip.py =========================
The router has the following interfaces:

  • GigabitEthernet1
  • GigabitEthernet2
  • GigabitEthernet3
  • Loopback100

Which Interface do you want to configure? GigabitEthernet2
Traceback (most recent call last):
File "/root/updateip.py", line 181, in
sys.exit(main())
File "/root/updateip.py", line 162, in main
selected_interface = interface_selection(interfaces)
File "/root/updateip.py", line 128, in interface_selection
while sel == args.interface or not sel in [intf["name"] for intf in interfaces]:
NameError: name 'args' is not defined

or

The router has the following interfaces:

  • GigabitEthernet1
  • GigabitEthernet2
  • GigabitEthernet3
  • Loopback100

Which Interface do you want to configure? 2
Traceback (most recent call last):
File "/root/updateip.py", line 181, in
sys.exit(main())
File "/root/updateip.py", line 162, in main
selected_interface = interface_selection(interfaces)
File "/root/updateip.py", line 128, in interface_selection
while sel == args.interface or not sel in [intf["name"] for intf in interfaces]:
NameError: name 'args' is not defined

Retrieving device serial number from Cisco switch

I want my script to 1) login to a Cisco switch and do a "show inventory" command (done successfully so far) 2) save the output of "SN: FOCxxxxxxxx" in memory instead of saving it to a local file.

I want to do this in order to be able to match each serial number to a particular IP address.

Any ideas what I can do to achieve this?

Thanks!
Tiam

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.