...
https://github.com/dahai0013/Basic-python-with-Juniper/tree/master/python
| On Ubuntu server ( not Windows ) |
---|
Documentation | https://www.juniper.net/documentation/en_US/junos-pyez/topics/reference/general/junos-pyez-configuration-process-and-data-formats.html |
change system message |
Code Block |
---|
| labuser@saltsackmaster:~/project/python_script$ more chg_system_message.py
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from pprint import pprint
junos_hosts = [ '172.30.95.177' ]
for ip in junos_hosts:
## open session with juniper divice
dev = Device(host=ip, user='Netbox', password='Netbox')
dev.open()
## Add or bound the configuration instance variable to the dev instan
dev.bind(cu=Config)
## change config
cmd_chg_hostname='set system login message "test jlk from python"'
result = dev.cu.load(cmd_chg_hostname,format='set')
pprint (result)
dev.cu.pdiff()
## commit changes
dev.cu.commit()
## close the session
dev.close()
|
|
disable interface |
Code Block |
---|
title | python disable interface |
---|
collapse | true |
---|
| labuser@saltsackmaster:~/project/python_script$ more disable_int.py
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from pprint import pprint
junos_hosts = [ '172.30.95.177' ]
for ip in junos_hosts:
## open session with juniper divice
dev = Device(host=ip, user='Netbox', password='Netbox')
dev.open()
## Add or bound the configuration instance variable to the dev instan
dev.bind(cu=Config)
## change config
cmd_chg_hostname='set interfaces xe-0/0/10 disable'
#cmd_chg_hostname='delete interfaces xe-0/0/10 disable'
result = dev.cu.load(cmd_chg_hostname,format='set')
pprint (result)
dev.cu.pdiff()
## commit changes
dev.cu.commit()
## close the session
dev.close()
|
|
run script and output |
Code Block |
---|
title | run python script and ouput |
---|
collapse | true |
---|
| labuser@saltsackmaster:~/project/python_script$ python3 disable_int.py
<Element load-configuration-results at 0x7f2ba17205c8>
[edit interfaces]
+ xe-0/0/10 {
+ disable;
+ }
labuser@saltsackmaster:~/project/python_script$
On the QFX5100:
---------------
{master:0}
root@QFX5100-1-RL102> show configuration interfaces xe-0/0/10
{master:0}
root@QFX5100-1-RL102> show configuration interfaces xe-0/0/10
disable;
{master:0}
root@QFX5100-1-RL102>
|
|
|
Code Block |
---|
title | get facts |
---|
collapse | true |
---|
| labuser@saltsackmaster:~/project/python_script$ more get_facts.py
from jnpr.junos import Device
from pprint import pprint
junos_hosts = [ '172.30.95.177' ]
# junos_hosts = ['172.30.92.204','172.30.92.213']
for ip in junos_hosts:
# dev = Device(host=ip, user='root', password='eyeno1t2!')
dev = Device(host=ip, user='Netbox', password='Netbox')
dev.open()
pprint(dev.facts)
print("\nJLK: version :",dev.facts['version'])
dev.close()
|
Code Block |
---|
title | facts with pyez |
---|
collapse | true |
---|
| import sys
import json
import yaml
from pprint import pprint
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError
dev = Device(host='192.168.99.11',user='ansible',passwd='ansible123')
try:
dev.open()
except ConnectError as err:
print ("Cannot connect to device: {0}".format(err))
sys.exit(1)
#print("#print everything\n")
#pprint (dev.facts)
#print("\n#print only the version")
#pprint (dev.facts['version'])
print (yaml.dump(dev.facts))
#print (json.dumps(dev.facts))
dev.close() |
|
|
|