-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-ospf.py
More file actions
51 lines (45 loc) · 1.73 KB
/
example-ospf.py
File metadata and controls
51 lines (45 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import yaml
from getpass import getpass
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.op.ospf import OspfNeighborTable
from time import sleep
hosts = ["crpd01","crpd02"]
junos_username = "your username" #please modify
#junos_password = getpass("Junos OS password: ")
junos_password = #provide your password or uncomment above line
def load_ospf_config(host):
#with open("ospf-{}.yaml".format(host)) as f:
with open("host_vars/{}.yml".format(host)) as f:
vars = yaml.load(f)
with Device(host=host, user=junos_username, passwd=junos_password, port=22) as dev:
with Config(dev, mode="exclusive") as conf:
conf.load(template_path="templates/ospf.j2", template_vars=vars, format='text')
conf.pdiff()
if conf.diff() is not None:
conf.commit()
def get_full_neighbor_count(host):
count = 0
with Device(host=host, user=junos_username, passwd=junos_password, port=22) as dev:
ospf_table = OspfNeighborTable(dev).get()
for neighbor in ospf_table:
if neighbor.ospf_neighbor_state == "Full":
count += 1
return count
if __name__ == "__main__":
for host in hosts:
print("Loading configuration on {}; the diff is:".format(host))
load_ospf_config(host)
ok_hosts = set()
for _ in range(20):
print("Waiting for OSPF to come up...")
sleep(5)
for host in hosts:
if host not in ok_hosts:
if get_full_neighbor_count(host) == 1:
ok_hosts.add(host)
if len(ok_hosts) == len(hosts):
print("Success!")
break
else:
print("Something went wrong, please check.")