published

netconfBulkOps

netconfBulkOps is a Python script for NETCONF bulk operations (get and edit-config) implemented as a minimum viable product (MVP).

A simple inventory file is used to describe the devices. A second file, containing either the NETCONF configuration payload or a subtree-filter, will then be applied to every device in the inventory. Instead of a subtree-filter or configuraton payload a xPath can be used. The results of the bulk operation is stored in corresponding files.

netconfBulkOps overview

netconfBulkOps is executed asynchronously using Python's concurrent.futures ThreadPoolExecutor and is therefore pretty fast.

Installation

Python based installation

Clone the repo

git clone https://github.com/spie-ics-ag/netconfBulkOps.git

Go to your project folder

cd netconfBulkOps

Set up a Python venv

First make sure that you have Python 3 installed on your machine. We will then be using venv to create an isolated environment with only the necessary packages.

Install virtualenv via pip

pip install virtualenv

Create the venv

python3 -m venv .venv

Activate your venv

source .venv/bin/activate

Install dependencies

pip install pip --upgrade
pip install -r requirements.txt

Configure the NETCONF username and password used to connect to your devices as environment variables

export NCBO_USER=your_username
export NCBO_PASSWORD=your_password

Docker based installation

Clone the repo

git clone https://github.com/spie-ics-ag/netconfBulkOps.git

Go to your project folder

cd netconfBulkOps

Configure the NETCONF username and password used to connect to your devices as environment variables

export NCBO_USER=your_username
export NCBO_PASSWORD=your_password

Build the docker image

docker build -t ncbo .

Run and enter the container

docker run -it -e NCBO_USER -e NCBO_PASS ncbo sh

In order to copy your [DEVICES] and [FILTER]/[CONFIG] files (see usage below) to the container you may use the docker cp command.

Usage

Preparation

Create a inventory file with all devices you want to connect to. Every line in the file represents a hostname or IP address.

Example

10.90.255.235
rtr01.internal.domain.com
sw01.internal.domain.com
10.90.255.245

Read data (NETCONF get) using a subtree-filter

Create a file containing the subtree-filter you want to use (without the <filter></filter> tags).

Example

<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
    <hostname/>
</native>

Start the script with the netconfBulkOps.py read [FILTER] [DEVICES] command. FILTER is your subtree-filter file and DEVICES is the inventory file. If you don't specify these parameters bulk_filter.xml is used as the default filter file and devices.txt is used as the default inventory file.

Usage: netconfBulkOps.py read [FILTER] [DEVICES]

  Retrive configuration and state information from all devices in the DEVICES
  file according the subtree filter in the FILTER file.

After the script is completed, you'll find a file per device containing the results in the output directory (output/out_read_[DEVICE NAME OR IP].xml).

Read data (NETCONF get) using xPath

Start the script with the netconfBulkOps.py xpath [XPATH] [DEVICES] command. XPATH is your xpath-string and DEVICES is the inventory file (devices.txt is used as the default inventory file).

Usage: netconfBulkOps.py xpath [XPATH] [DEVICES]

  Retrive configuration and state information from all devices in the
  DEVICES file according the XPATH filter.

After the script is completed, you'll find a file per device containing the results in the output directory (output/out_read_[DEVICE NAME OR IP].xml).

Write data (NETCONF edit-config)

Create a file containing the configuration you want to apply (outermost tag must be <config></config>).

Example

<config>
  <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
    <banner>
      <motd>
        <banner>Welcome to this device. Have a nice day!</banner>
      </motd>
    </banner>
  </native>
</config>

Start the script with the netconfBulkOps.py write [CONFIG] [DEVICES] command. CONFIG is the file containing the configuration and DEVICES is the inventory file. If you don't specify these parameters bulk_config.xml is used as the default config file and devices.txt is used as the default inventory file.

Usage: netconfBulkOps.py write [CONFIG] [DEVICES]

  Loads the configuration specified in the CONFIG file to the running
  datastore of all devices in the DEVICES file.

After the script is completed, a HTML report about the configuration results is generated. This report is available as config_report.html in the output directory.

Example report

Sandboxes

You may test this script using the following DevNet sandboxes:

View code on GitHub
  • Owner

  • Contributors

    +2Github contributors
  • Categories

  • Programming Languages

    Python
  • License

    MIT License

Code Exchange Community

Get help, share code, and collaborate with other developers in the Code Exchange community.View Community
Disclaimer:
Cisco provides Code Exchange for convenience and informational purposes only, with no support of any kind. This page contains information and links from third-party websites that are governed by their own separate terms. Reference to a project or contributor on this page does not imply any affiliation with or endorsement by Cisco.