Skip to content

Instantly share code, notes, and snippets.

@jeffbrl
Created March 10, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffbrl/2052351af1eb441b767404aac5036397 to your computer and use it in GitHub Desktop.
Save jeffbrl/2052351af1eb441b767404aac5036397 to your computer and use it in GitHub Desktop.
PyEZ config filtering example
# 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