Created
March 10, 2017 12:31
-
-
Save jeffbrl/2052351af1eb441b767404aac5036397 to your computer and use it in GitHub Desktop.
PyEZ config filtering example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pulled from Google Groups post by Stacy Smith | |
# https://groups.google.com/forum/#!topic/junos-python-ez/1OfnulLkyn4 | |
import sys | |
import getpass | |
from lxml import etree | |
from jnpr.junos.device import Device | |
# Python3 is input(), Python2 is raw_input() | |
try: | |
input = raw_input | |
except: | |
pass | |
username = input('Device Username: ') | |
passwd = getpass.getpass('Device Password: ') | |
prefix_list = input('Prefix List Name: ') | |
for hostname in sys.argv[1:]: | |
print("Connecting to %s..." % hostname) | |
dev = Device(host=hostname, user=username, password=passwd, port=22) | |
dev.open() | |
filter_string = etree.XML('<policy-options><prefix-list><name>%s' | |
'</name></prefix-list></policy-options>' % (prefix_list)) | |
resp = dev.rpc.get_config(filter_xml=filter_string, | |
options={'database': 'committed', | |
'inherit': 'inherit', | |
'commit-scripts': 'apply', }) | |
xpath = 'policy-options/prefix-list[name="%s"]/prefix-list-item' % (prefix_list) | |
items = resp.findall(xpath) | |
count = 0 | |
if items is not None: | |
count = len(items) | |
print("Prefix list %s contains %d IPs." % (prefix_list,count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment