RPC to Count IPs in Prefix List

162 views
Skip to first unread message

bri...@seas.upenn.edu

unread,
Mar 9, 2017, 11:30:19 AM3/9/17
to Junos Python EZ
Model: ex2200-c-12t-2g
Junos: 14.1X53-D40.8


I'm looking for an RPC I can use to display to number of IPs in a prefix list. The equivalent of:

dev.cli("show configuration poilicy-options prefix-lists [PREFIX-LIST-NAME] | count")


show configuration policy-options prefix-lists | display xml rpc

is not a valid command. 


Nitin Kumar

unread,
Mar 9, 2017, 11:56:29 AM3/9/17
to bri...@seas.upenn.edu, Junos Python EZ

For configurational data we have get_config RPC.

 

You can do for example

 

op = dev.rpc.get_config(filter_xml = etree.XML('<policy-options><prefix-list/></policy-options>'))
print etree.tostring(op)

 

How did I concluded above API, so below is the command on device which helped me

# show policy-options prefix-list test | display xml

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/17.2D0/junos">

    <configuration junos:changed-seconds="1489078296" junos:changed-localtime="2017-03-09 22:21:36 IST">

            <policy-options>

                <prefix-list>

                    <name>test</name>

                </prefix-list>

            </policy-options>

    </configuration>

Thanks

Nitin Kr

--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-...@googlegroups.com.
Visit this group at https://groups.google.com/group/junos-python-ez.
To view this discussion on the web visit https://groups.google.com/d/msgid/junos-python-ez/4d72f6d0-9084-422b-8153-ac000ceeb641%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stacy W. Smith

unread,
Mar 9, 2017, 12:35:52 PM3/9/17
to bri...@seas.upenn.edu, Junos Python EZ, Nitin Kumar
To expand on Nitin's suggestion...

Here's an example script that prompts for the prefix list name and prints the number of IPs in the prefix list. It works from the committed configuration after any group or commit script expansion:

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)) 

--Stacy


bri...@seas.upenn.edu

unread,
Mar 9, 2017, 1:26:43 PM3/9/17
to Junos Python EZ, bri...@seas.upenn.edu, nit...@juniper.net
Thanks Nitin!

bri...@seas.upenn.edu

unread,
Mar 9, 2017, 1:28:14 PM3/9/17
to Junos Python EZ, bri...@seas.upenn.edu, nit...@juniper.net
Very useful. Thanks for your help!
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-ez+unsub...@googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-ez+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages