Junos Automation (Scripting)
Junos Automation (Scripting)

How to create a table & view

[ Edited ]
‎10-24-2016 05:59 AM

Hello 

 

Can someone point me in the right direction. I want to learn more about creating Table & Views with Yaml. I want to be able to get the Seculity ALG status.

 

Here what i have but its not working

 

 

---
AlgTable:
rpc: get-alg-status
item: alg-status
view: AlgView

AlgView:
fields:
alg-status: alg-dns-status

 Here my Code 

def Login_in(self):
Hostname = input("Device hostname: ")
Username = input("Device username: ")
Password = getpass("Device password: ")
dev = Device(host=Hostname, user= Username, passwd= Password)
try:
dev.open()
eths = EthPortTable(dev).get()
ag = AlgTable(dev).get()

for port in eths:
print("{}: {}: {}".format(port.name, port.oper, port.admin))

print(ag)

except Exception as err:
print(err)
sys.exit(1)




return

 I have created to files one Yaml and one Python. Does anyone know of a good documnets that explain how to get the data.

I have read the documnets on Juniper site but still don't understand.  

 

Attachments

5 REPLIES 5
Junos Automation (Scripting)

Re: How to create a table & view

‎10-24-2016 09:31 AM

What is the error?  Did you define the table in-line or is it in a separate file?  If it's the latter, did you create the YAML formatter/converter?  Did you import it?  If it's the former, is the T&V defined globally?  

 

A lot of different ways to do this, so need to see ALL of the code after you share the error.  

Edward Arcuri
Architect - Automation & Programmability, JDI
Junos Automation (Scripting)

Re: How to create a table & view

‎10-24-2016 10:43 AM

Earcurl 

 

 

i created two files one yaml and one .py

 

here are the two files contect Yamal

 

---
AlgTable:
rpc: get-alg-status
item: alg-dns-status
view: AlgView

AlgView:
fields:
DNS: alg-status

 

Python file 

 

"""
Pythonifier for Alg Table/View
"""
from jnpr.junos.factory import loadyaml
from os.path import splitext
_YAML_ = splitext(__file__)[0] + '.yml'
globals().update(loadyaml(_YAML_))

 

 

If you know of some good documenation that i could read that would be great.

 

Thank you 

 

 

 

 

 

Thank you  

 

Highlighted
Junos Automation (Scripting)

Re: How to create a table & view

‎10-25-2016 02:45 AM

We can also use user-defined in the script itself. Check below link for an example

 

https://github.com/vnitinv/pyez-examples/blob/master/9_user_defined_table.py 

 

So in your case code would look like below

 

from jnpr.junos import Device
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml

yaml_data="""
---

AlgTable:
  rpc: get-alg-status
  item: alg-dns-status
  view: AlgView

 

AlgView:
  fields:
    DNS: alg-status


"""

dev = Device(host='xxxx', user='xxxx', password='xxx', gather_facts=False)
dev.open()

globals().update(FactoryLoader().load(yaml.load(yaml_data)))
ags =AlgTable(dev)
ags.get()
for ag in ags:
    print 'dns: ', ag.DNS
    print

 

dev.close()

Junos Automation (Scripting)

Re: How to create a table & view

[ Edited ]
‎10-25-2016 02:28 PM

I try this and i got nothing to print

 

from jnpr.junos import Device
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml

yaml_data = """
---
AlgTable:
rpc: get-alg-status
item: alg-dns-status
view: AlgView

AlgView:
fields:
DNS: alg-status

"""
dev = Device(host='172.20.1.201', user='root', password='Password', gather_facts=False)
dev.open()
globals().update(FactoryLoader().load(yaml.load(yaml_data)))
ags = AlgTable(dev)
ags.get()
for ag in ags:
print(
'dns: ', ag.DNS)


dev.close()

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/edwardmanning/PycharmProjects/python3.4/ed.py
dns: None

Process finished with exit code 0

It does not seem to be loading and records 

 

What am I doing wrong

 

Junos Automation (Scripting)

Re: How to create a table & view

‎11-13-2016 02:48 AM

Hi

 

If you want to get DNS ALG status with PyEZ Tables, this is possible by slightly modifying your script as follows:

 

from jnpr.junos import Device
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml

yaml_data = """
---
AlgTable:
  rpc: get-alg-status
  item: alg-dns-status
  view: AlgView

AlgView:
  fields:
    DNS: .

"""
dev = Device(host='172.20.1.201', user='root', password='Password', gather_facts=False)
dev.open()
globals().update(FactoryLoader().load(yaml.load(yaml_data)))
ags = AlgTable(dev)
ags.get()
for ag in ags:
    print(
    'dns: ', ag.DNS)

dev.close()

The result:

 

 

dns:  enabled

 

 

However I would say this is not the best use of Tables/Views. The main use case for Tables/Views is when you have several items with same XML tag, such as many interfaces or routes. In this case we just have only one alg-dns-status element in any case, so it is easier to just call the rpc and parse the response:

 

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X44/junos">
    <alg-status xmlns="http://xml.juniper.net/junos/12.1X44/junos-alg">
        <alg-dns-status>enabled</alg-dns-status>
        <alg-ftp-status>enabled</alg-ftp-status>
        <alg-h323-status>enabled</alg-h323-status>
        <alg-mgcp-status>enabled</alg-mgcp-status>
        <alg-msrpc-status>enabled</alg-msrpc-status>
        <alg-pptp-status>enabled</alg-pptp-status>
        <alg-rsh-status>enabled</alg-rsh-status>
        <alg-rtsp-status>enabled</alg-rtsp-status>
        <alg-sccp-status>enabled</alg-sccp-status>
        <alg-sip-status>enabled</alg-sip-status>
        <alg-sql-status>enabled</alg-sql-status>
        <alg-sunrpc-status>enabled</alg-sunrpc-status>
        <alg-talk-status>enabled</alg-talk-status>
        <alg-tftp-status>enabled</alg-tftp-status>
        <alg-ike-esp-nat-status>disabled</alg-ike-esp-nat-status>
    </alg-status>
    <cli>
        <banner></banner>
    </cli>
</rpc-reply>

More info on Tables and Views is here.

 

 

Best Regards,
PK

Juniper Ambassador, Juniper Networks Certified Instructor,
JNCIE-SEC #98, JNCIE-ENT #393, JNCIE-SP #2253
Twitter: @JuniperTrain
GitHub: https://github.com/pklimai
[Juniper Authorized Education & Support in Russia]