diff --git a/pyth_ans_ecourse/juniper_ansible/ansible-hosts b/pyth_ans_ecourse/juniper_ansible/ansible-hosts new file mode 100644 index 0000000..aef73ba --- /dev/null +++ b/pyth_ans_ecourse/juniper_ansible/ansible-hosts @@ -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 + diff --git a/pyth_ans_ecourse/juniper_ansible/ex1_facts.yml b/pyth_ans_ecourse/juniper_ansible/ex1_facts.yml new file mode 100644 index 0000000..98cabf1 --- /dev/null +++ b/pyth_ans_ecourse/juniper_ansible/ex1_facts.yml @@ -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 }}" diff --git a/pyth_ans_ecourse/juniper_ansible/ex2_hostname_conf.yml b/pyth_ans_ecourse/juniper_ansible/ex2_hostname_conf.yml new file mode 100644 index 0000000..18caf29 --- /dev/null +++ b/pyth_ans_ecourse/juniper_ansible/ex2_hostname_conf.yml @@ -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 }} + diff --git a/pyth_ans_ecourse/juniper_ansible/ex3_hostname_xml.yml b/pyth_ans_ecourse/juniper_ansible/ex3_hostname_xml.yml new file mode 100644 index 0000000..d80e349 --- /dev/null +++ b/pyth_ans_ecourse/juniper_ansible/ex3_hostname_xml.yml @@ -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 }} + diff --git a/pyth_ans_ecourse/juniper_ansible/set_hostname.conf b/pyth_ans_ecourse/juniper_ansible/set_hostname.conf new file mode 100644 index 0000000..0d9bf08 --- /dev/null +++ b/pyth_ans_ecourse/juniper_ansible/set_hostname.conf @@ -0,0 +1,4 @@ + +system { + host-name pynet-test1; +} diff --git a/pyth_ans_ecourse/juniper_ansible/set_hostname.xml b/pyth_ans_ecourse/juniper_ansible/set_hostname.xml new file mode 100644 index 0000000..2942fb2 --- /dev/null +++ b/pyth_ans_ecourse/juniper_ansible/set_hostname.xml @@ -0,0 +1,5 @@ + + + pynet-jnpr-srx1 + + diff --git a/pyth_ans_ecourse/juniper_class/ex1_display_facts.py b/pyth_ans_ecourse/juniper_class/ex1_display_facts.py new file mode 100755 index 0000000..3aa9f0f --- /dev/null +++ b/pyth_ans_ecourse/juniper_class/ex1_display_facts.py @@ -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() diff --git a/pyth_ans_ecourse/juniper_class/ex2_eth_stats.py b/pyth_ans_ecourse/juniper_class/ex2_eth_stats.py new file mode 100755 index 0000000..24d3b26 --- /dev/null +++ b/pyth_ans_ecourse/juniper_class/ex2_eth_stats.py @@ -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() diff --git a/pyth_ans_ecourse/juniper_class/ex3_route_table.py b/pyth_ans_ecourse/juniper_class/ex3_route_table.py new file mode 100755 index 0000000..0233eac --- /dev/null +++ b/pyth_ans_ecourse/juniper_class/ex3_route_table.py @@ -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() diff --git a/pyth_ans_ecourse/juniper_class/ex4_change_hostname.py b/pyth_ans_ecourse/juniper_class/ex4_change_hostname.py new file mode 100755 index 0000000..0b448bc --- /dev/null +++ b/pyth_ans_ecourse/juniper_class/ex4_change_hostname.py @@ -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() diff --git a/pyth_ans_ecourse/juniper_class/load_hostname.conf b/pyth_ans_ecourse/juniper_class/load_hostname.conf new file mode 100644 index 0000000..8f5b09a --- /dev/null +++ b/pyth_ans_ecourse/juniper_class/load_hostname.conf @@ -0,0 +1,3 @@ +system { + host-name test2; +} diff --git a/pyth_ans_ecourse/juniper_class/load_hostname.xml b/pyth_ans_ecourse/juniper_class/load_hostname.xml new file mode 100644 index 0000000..6499c53 --- /dev/null +++ b/pyth_ans_ecourse/juniper_class/load_hostname.xml @@ -0,0 +1,6 @@ + + + pynet-jnpr-srx1 + + +