cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1658
Views
10
Helpful
2
Replies

How to use netconf script for multiple devices

menzies456
Level 1
Level 1

hi,

i'm trying to work out how to call a list of routers for a netconf python script samtest.py,

very simply shown here:

Here is my list of routers:

router_info.py

router = {"host""172.16.8.100",
          "port""830",
          "username""xx",
          "password""xx"}

router = {"host""172.16.8.200",
          "port""22",
          "username""xx",
          "password""xx"}
samtest.py
from ncclient import manager
from pprint import pprint
import xmltodict
from router_info import router

with manager.connect(host=router["host"], port=router["port"], username=router["username"], password=router["password"], hostkey_verify=False) as m:
 for capability in m.server_capabilities:
   print(capability)
 
At the moment this only shows a single router, what i need is a for loop but i'm not sure how best to do this.....
 
If I change the script to this it works but this is not scalable:
from router_info import router
from router_info import router2

with manager.connect(host=router["host"], port=router["port"], username=router["username"], password=router["password"], hostkey_verify=False) as m:
 for capability in m.server_capabilities:
   print(capability)

with manager.connect(host=router2["host"], port=router2["port"], username=router2["username"], password=router2["password"], hostkey_verify=False) as m:
 for capability in m.server_capabilities:
   print(capability)
 
Thanks
Sam
 
2 Replies 2

There is a couple of ways to do this. Like your example, I did this with Netmiko, you can create your devices and list all in here.

 

devices = [{
    "device_type": "cisco_xr",
    "ip": "sbx-iosxr-mgmt.cisco.com",
    "username": "admin",
    "password": "C1sco12345",
    "port": "8181",
}, {
    "device_type": "cisco_xe",
    "ip": "ios-xe-mgmt-latest.cisco.com",
    "username": "developer",
    "password": "C1sco12345",
    "port": "8181",
}]

for device in devices:

The only way is to use an inventory file, with JSON or YAML, for example this JSON file has a few routers in and what they do in the network - in this case some were route reflectors and other clients

 

{
  "router:1": {
    "IP": "10.94.242.179",
    "type": "iosxr",
    "user": "cisco",
    "password": "cisco",
    "rr": "reflector"
  },
  "router:2": {
    "IP": "10.94.242.171",
    "type": "iosxr",
    "user": "cisco",
    "password": "cisco",
    "rr": "client"
  },
  "router:3": {
    "IP": "10.94.242.172",
    "type": "iosxr",
    "user": "cisco",
    "password": "cisco",
    "rr": "client"
 }
}

In the code, pass in the JSON file (I did this with a try) then followed with a for loop

 

try:
        with open(args.inventory, "r") as f:
            device_data = json.load(f)
    except (ValueError, IOError, OSError) as err:
        merge_device("Could not read the 'devices' file:", err)

You will find some good examples like this on https://developer.cisco.com/codeexchange

 

Hope this helps.

 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

menzies456
Level 1
Level 1

Thanks!