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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# Copyright (c) 2016 Hochikong
from pxssh import pxssh
from .mgmt_common import messenger
class Router(object):
def __init__(self, address, cred):
"""Initial a router object
:param address: Router address,example:'192.168.10.10'
:param cred: Router user and password,example:'vyos:vyos'
"""
self.__address = address
self.__cred = list(cred)
self.__divi = self.__cred.index(":")
self.__username = ''.join(self.__cred[:self.__divi])
self.__passwd = ''.join(self.__cred[self.__divi+1:])
self.__conn = pxssh()
self.__status = {"status": None, "commit": None, "save": None, "configure": None}
self.__basic_string = {0: 'set ', 1: 'delete '}
def status(self):
"""Check the router object inner status
:return: A python dictionary include the status of the router object
"""
return self.__status
def login(self):
"""Login the router
:return: A message or an error
"""
try:
if self.__conn.login(self.__address, self.__username, self.__passwd) is True:
self.__status["status"] = "login"
return "Result : Login successfully."
else:
return "Error : Connect Failed."
except Exception as e:
return e
def logout(self):
"""Logout the router
:return: A message or an error
"""
try:
self.__conn.close()
self.__status["status"] = "logout"
self.__status["configure"] = None
self.__conn = pxssh()
return "Result : Logout successfully."
except Exception as e:
return e
def configure(self):
"""Enter the VyOS configure mode
:return: A message or an error
"""
try:
if self.__status["status"] == "login":
if self.__status["configure"] is not "Yes":
self.__conn.sendline("configure")
self.__conn.prompt(0)
self.__conn.set_unique_prompt()
self.__status["configure"] = "Yes"
return "Result : Active configure mode successfully."
else:
return "Error : In configure mode now!"
else:
return "Error : Router object not connect to a router."
except Exception as e:
return e
def commit(self):
"""Commit the configuration changes
:return: A message or an error
"""
try:
if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
if self.__status["commit"] is None:
return "Error : You don't need to commit."
if self.__status["commit"] == "No":
self.__conn.sendline("commit")
self.__conn.prompt()
self.__status["commit"] = "Yes"
return "Result : Commit successfully."
else:
return "Error : You have committed!"
else:
return "Error : Router not in configure mode!"
else:
return "Error : Router object not connect to a router."
except Exception as e:
return e
def save(self):
"""Save the configuration after commit
:return: A message or an error
"""
try:
if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
if self.__status["commit"] == "Yes":
if self.__status["save"] is None:
return "Error : You don't need to save."
if self.__status["save"] == "No":
self.__conn.sendline("save")
self.__conn.prompt(0)
self.__status["save"] = "Yes"
return "Result : Save successfully."
else:
return "Error : You have saved!"
elif self.__status["commit"] is None:
return "Error : You don't need to save."
else:
return "Error : You need to commit first!"
else:
return "Error : Router not in configure mode!"
else:
return "Error : Router object not connect to a router."
except Exception as e:
return e
def exit(self, force=False):
"""Exit VyOS configure mode
:param force: True or False
:return: A message or an error
"""
try:
if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
if force is True:
self.__conn.sendline("exit discard")
self.__conn.prompt()
self.__status["configure"] = "No"
self.__status["save"] = None
self.__status["commit"] = None
return "Result : Exit configure mode successfully."
else:
if self.__status["commit"] == "Yes":
if self.__status["save"] == "Yes":
self.__conn.sendline("exit")
self.__conn.prompt()
self.__status["configure"] = "No"
self.__status["save"] = None
self.__status["commit"] = None
return "Result : Exit configure mode successfully."
else:
return "Error : You should save first."
elif self.__status["commit"] is None:
self.__conn.sendline("exit")
self.__conn.prompt()
self.__status['configure'] = "No"
return "Result : Exit configure mode successfully."
else:
return "Error : You should commit first."
else:
return "Error : You are not in configure mode,need not exit."
else:
return "Error : Router object not connect to a router."
except Exception as e:
return e
def set(self, config):
"""Basic 'set' method,execute the set command in VyOS
:param config: A configuration string.
e.g. 'protocols static route ... next-hop ... distance ...'
:return: A message or an error
"""
full_config = self.__basic_string[0] + config
try:
if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = messenger(self.__conn, full_config)
if "Result" in res:
if self.__status["commit"] == "No":
pass
else:
self.__status["commit"] = "No"
if self.__status["save"] == "No":
pass
else:
self.__status["save"] = "No"
return res
else:
return res
else:
return "Error : You are not in configure mode."
else:
return "Error : Router object not connect to a router."
except Exception as e:
return e
def delete(self, config):
"""Basic 'delete' method,execute the delete command in VyOS
:param config: A configuration string.
e.g. 'protocols static route ... next-hop ... distance ...'
:return: A message or an error
"""
full_config = self.__basic_string[1] + config
try:
if self.__status["status"] == "login":
if self.__status["configure"] == "Yes":
res = messenger(self.__conn, full_config)
if "Result" in res:
if self.__status["commit"] == "No":
pass
else:
self.__status["commit"] = "No"
if self.__status["save"] == "No":
pass
else:
self.__status["save"] = "No"
return res
else:
return res
else:
return "Error : You are not in configure mode."
else:
return "Error : Router object not connect to a router."
except Exception as e:
return e
|