-
Notifications
You must be signed in to change notification settings - Fork 546
/
provision.py
executable file
·101 lines (78 loc) · 2.5 KB
/
provision.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
import sys
from pyIOSXR import IOSXR
import pyeapi
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
import jnpr.junos.exception
import pexpect
import httplib
def print_info_message():
print(
"BOX is no longer reachable with vagrant up. Use ssh (check the IP in the initial conf)"
)
print("Don't forget to change the network type of the first NIC of the box.")
def provision_iosxr(port, username, password):
device = IOSXR(
hostname="127.0.0.1", username=username, password=password, port=port
)
device.open()
device.load_candidate_config(filename="../iosxr/initial.conf")
try:
device.commit_replace_config()
except pexpect.TIMEOUT:
# This actually means everything went fine
print_info_message()
def provision_eos(port, username, password):
connection = pyeapi.client.connect(
transport="https",
host="localhost",
username="vagrant",
password="vagrant",
port=port,
)
device = pyeapi.client.Node(connection)
commands = list()
commands.append("configure session")
commands.append("rollback clean-config")
with open("../eos/initial.conf", "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line == "":
continue
if line.startswith("!"):
continue
commands.append(line)
commands[-1] = "commit"
try:
device.run_commands(commands)
except httplib.BadStatusLine:
# This actually means everything went fine
print_info_message()
def provision_junos(port, username, password):
device = Device("127.0.0.1", user=username, port=port)
device.open()
device.bind(cu=Config)
with open("../junos/initial.conf", "r") as f:
configuration = f.read()
device.cu.load(configuration, format="text", overwrite=True)
try:
device.cu.commit()
device.close()
except jnpr.junos.exception.RpcTimeoutError:
# This actually means everything went fine
print_info_message()
if __name__ == "__main__":
os = sys.argv[1]
port = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]
if os == "iosxr":
provision_iosxr(port, username, password)
elif os == "eos":
provision_eos(port, username, password)
elif os == "junos":
provision_junos(port, username, password)