Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ktbyers/pynet
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Oct 14, 2015
2 parents 8c08abe + f4e83e5 commit 1dbfcf5
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pyth_ans_ecourse/juniper_ansible/ansible-hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[local]
localhost ansible_connection=local

[juniper]
pynet-sf-srx ansible_ssh_host=10.10.10.227 juniper_user=pyclass juniper_passwd=password

[juniper:vars]
ansible_connection=local
ansible_python_interpreter=~/applied_python/bin/python

11 changes: 11 additions & 0 deletions pyth_ans_ecourse/juniper_ansible/ex1_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Print out Juniper Facts
hosts: juniper
gather_facts: false
tasks:
- name: Retrieve Juniper Facts
junos_get_facts: host={{ ansible_ssh_host }} user={{ juniper_user }} passwd={{ juniper_passwd }}
register: srx_info

- name: Print SRX information
debug: msg="{{ srx_info.facts }}"
13 changes: 13 additions & 0 deletions pyth_ans_ecourse/juniper_ansible/ex2_hostname_conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: Set hostname using conf notation
hosts: juniper
gather_facts: false
tasks:
- name: Set system hostname
junos_install_config:
host={{ ansible_ssh_host }}
file=set_hostname.conf
overwrite=false
user={{ juniper_user }}
passwd={{ juniper_passwd }}

13 changes: 13 additions & 0 deletions pyth_ans_ecourse/juniper_ansible/ex3_hostname_xml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: Set hostname using XML
hosts: juniper
gather_facts: false
tasks:
- name: Set system hostname
junos_install_config:
host={{ ansible_ssh_host }}
file=set_hostname.xml
overwrite=false
user={{ juniper_user }}
passwd={{ juniper_passwd }}

4 changes: 4 additions & 0 deletions pyth_ans_ecourse/juniper_ansible/set_hostname.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

system {
host-name pynet-test1;
}
5 changes: 5 additions & 0 deletions pyth_ans_ecourse/juniper_ansible/set_hostname.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<configuration>
<system>
<host-name>pynet-jnpr-srx1</host-name>
</system>
</configuration>
33 changes: 33 additions & 0 deletions pyth_ans_ecourse/juniper_class/ex1_display_facts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
'''
Connect to Juniper device using PyEZ. Display device facts.
'''

from jnpr.junos import Device
from getpass import getpass
from pprint import pprint


def main():
'''
Connect to Juniper device using PyEZ. Display device facts.
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()

juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}

print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()
pprint(a_device.facts)
print


if __name__ == "__main__":
main()
45 changes: 45 additions & 0 deletions pyth_ans_ecourse/juniper_class/ex2_eth_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
'''
Connect to Juniper device using PyEZ. Display operational state and pkts_in, pkts_out for all
of the interfaces.
'''

from jnpr.junos import Device
from jnpr.junos.op.ethport import EthPortTable
from getpass import getpass


def main():
'''
Connect to Juniper device using PyEZ. Display operational state and pkts_in, pkts_out for all
of the interfaces.
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()

juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}

print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()

eth_ports = EthPortTable(a_device)
eth_ports.get()

print "{:>15} {:>12} {:>12} {:>12}".format("INTF", "OPER STATE", "IN PACKETS", "OUT PACKETS")
for intf, eth_stats in eth_ports.items():
eth_stats = dict(eth_stats)
oper_state = eth_stats['oper']
pkts_in = eth_stats['rx_packets']
pkts_out = eth_stats['tx_packets']
print "{:>15} {:>12} {:>12} {:>12}".format(intf, oper_state, pkts_in, pkts_out)
print


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions pyth_ans_ecourse/juniper_class/ex3_route_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
'''
Connect to Juniper device using PyEZ. Display the routing table.
'''

from jnpr.junos import Device
from jnpr.junos.op.routes import RouteTable
from getpass import getpass


def main():
'''
Connect to Juniper device using PyEZ. Display the routing table.
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()

juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}

print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()

routes = RouteTable(a_device)
routes.get()

print "\nJuniper SRX Routing Table: "
for a_route, route_attr in routes.items():
print "\n" + a_route
for attr_desc, attr_value in route_attr:
print " {} {}".format(attr_desc, attr_value)

print "\n"


if __name__ == "__main__":
main()
68 changes: 68 additions & 0 deletions pyth_ans_ecourse/juniper_class/ex4_change_hostname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python
'''
Exercise using Juniper's PyEZ to make changes to device in various ways
Using the PyEZ load method set the hostname on the device using set, conf, and XML formats.
Display the differences between the running config and candidate config after each change.
Perform at least one commit and one rollback(0) during this process.
The hostname at the end of the testing should be: pynet-jnpr-srx1
'''

from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from getpass import getpass


def main():
'''
Exercise using Juniper's PyEZ to make changes to device in various ways
'''
pwd = getpass()
ip_addr = raw_input("Enter Juniper SRX IP: ")
ip_addr = ip_addr.strip()

juniper_srx = {
"host": ip_addr,
"user": "pyclass",
"password": pwd
}

print "\n\nConnecting to Juniper SRX...\n"
a_device = Device(**juniper_srx)
a_device.open()

cfg = Config(a_device)

print "Setting hostname using set notation"
cfg.load("set system host-name test1", format="set", merge=True)

print "Current config differences: "
print cfg.diff()

print "Performing rollback"
cfg.rollback(0)

print "\nSetting hostname using {} notation (external file)"
cfg.load(path="load_hostname.conf", format="text", merge=True)

print "Current config differences: "
print cfg.diff()

print "Performing commit"
cfg.commit()

print "\nSetting hostname using XML (external file)"
cfg.load(path="load_hostname.xml", format="xml", merge=True)

print "Current config differences: "
print cfg.diff()

print "Performing commit"
cfg.commit()
print


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions pyth_ans_ecourse/juniper_class/load_hostname.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
system {
host-name test2;
}
6 changes: 6 additions & 0 deletions pyth_ans_ecourse/juniper_class/load_hostname.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<configuration>
<system>
<host-name>pynet-jnpr-srx1</host-name>
</system>
</configuration>

0 comments on commit 1dbfcf5

Please sign in to comment.