Getting started with Genie/pyATS and IOS XR
To run the example on a container image, execute:
git clone https://github.com/nleiva/xr-genie.git && cd xr-genie
docker run -it --rm --name my-genie quay.io/nleiva/xr-genie
NOTE: They keep an official Cisco pyATS Docker image at https://hub.docker.com/r/ciscotestautomation/pyats/.
Once you are in the container, run the Python interpreter (interactive mode) with the command python3
to get immediate feedback for each statement.
$ docker run -it --rm --name my-genie quay.io/nleiva/xr-genie
root@9423f2d426d0:/# python3
Python 3.9.9 (main, Nov 22 2021, 00:00:00)
[GCC 11.2.1 20211019 (Red Hat 11.2.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Next, import the libraries required to run this example. Copy & paste this lines to the Python interpreter.
from ats.topology import loader
from genie.conf import Genie
from genie.libs.conf.interface.iosxr import Interface
import pprint
from genie.libs.parser.iosxr.show_interface import ShowIpInterfaceBrief
Load the testbed details. Use the example file in the repo or create your own. If you use the example file, just copy and paste the lines below in the Python interpreter.
pyats_testbed = loader.load('example/interfaces/testbed.yaml')
testbed = Genie.init(pyats_testbed)
Connect to the device(s). It is very important the device name(s) in the YAML definition file matches the hostname on the device’s prompt. If the DevNet alway-on IOS XR device hostname is different than "iosxr1", this example might not work.
device = testbed.devices['iosxr1']
device.connect()
Parse the output of show interfaces brief
using the Genie Parser. Take a look at some of the option available for IOS XR.
output = ShowIpInterfaceBrief(device=device)
parsed_output = output.parse()
pprint.pprint(parsed_output)
If you want to grab the IP address of given interface for example, you can go down the parsed_output
data tree:
>>> >>> parsed_output["interface"]["GigabitEthernet0/0/0/4"]["ip_address"]
'192.0.2.11'
Let's use Genie Conf, a library that provides objects that model the configuration of devices to configure an interface (library already imported in the statements initially).
intf1 = Interface(name='GigabitEthernet0/0/0/4', device=device)
intf1.description = 'test'
intf1.ipv4 = '203.0.113.11/24'
intf1.ipv6 = '2001:db8:ff::11/64'
cfgs = intf1.build_config()
You can either run a CLI command or repeat the previous process to parse individual fields.
>>> device.execute('show run int gi0/0/0/4')
2021-12-22 19:17:46,808: %UNICON-INFO: +++ iosxr1 with via 'vty': executing command 'show run int gi0/0/0/4' +++
show run int gi0/0/0/4
Wed Dec 22 19:16:16.970 UTC
interface GigabitEthernet0/0/0/4
description test
ipv4 address 203.0.113.11 255.255.255.0
ipv6 address 2001:db8:ff::11/64
shutdown
!
Parse the output of show interfaces brief
again.
output = ShowIpInterfaceBrief(device=device)
parsed_output = output.parse()
Then you get the IP from it.
>>> parsed_output["interface"]["GigabitEthernet0/0/0/4"]["ip_address"]
'203.0.113.11'
Finally, disconnect from the device to terminate the SSH connection.
device.disconnect()
The container instance is removed when it exits (option --rm
). To remove the container image completely from your system, you can run:
docker rmi quay.io/nleiva/xr-genie
A (Dockerfile) has been added to build the Conatiner image manually if you prefer to do so instead of pulling it from Quay. You can pre-download the image with docker pull quay.io/nleiva/xr-genie
.
To build the image you can either use buildah
:
buildah bud -t quay.io/nleiva/xr-genie .
Or docker
:
docker build -t quay.io/nleiva/xr-genie .