summaryrefslogtreecommitdiff
path: root/README.md
blob: 704551a62f0263255d2c81309235bd81250d3cb7 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#VyMGMT
A python library for VyOS configurations

This python library is used for VyOS configurations.  

Use this library to send the configuration commands to VyOS. 

##Note
###Version:0.1 
 
###Author:Hochikong  
 
###Contact me:hochikong@foxmail.com(usually use) or michellehzg@gmail.com(Inconvenient in Mainland China)  

###License:MIT   
  
###Requirement:pexpect(pxssh)  

###Platform:Python2 and 3

##Basic Example
Set a description for eth0:   

    >>> from vymgmt.router  import Router
	>>> vyos = Router('192.168.225.2','vyos','vyos')
	>>> vyos.login()
	>>> vyos.configure()
	>>> vyos._status()
	{'session_modified': False, 'logged_in': True, 'session_saved': True, 'conf_mode': True}
	>>> vyos.set("interfaces ethernet eth0 description 'interface_eth0'")
	>>> vyos._status()
	{'session_modified': True, 'logged_in': True, 'session_saved': True, 'conf_mode': True}
	>>> vyos.logout()
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/usr/local/lib/python3.5/dist-packages/vymgmt-0.1-py3.5.egg/vymgmt/router.py", line 90, in logout
	vymgmt.router.VyOSError: Cannot logout before exiting configuration mode
	>>> vyos.commit()
	>>> vyos.exit()
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/usr/local/lib/python3.5/dist-packages/vymgmt-0.1-py3.5.egg/vymgmt/router.py", line 174, in exit
	vymgmt.router.VyOSError: Cannot exit a session with unsaved changes, use force flag to ignore
	>>> vyos.save()
	>>> vyos.exit()
	>>> vyos.logout()
	>>> vyos._status()
	{'session_modified': False, 'logged_in': False, 'session_saved': True, 'conf_mode': False}

Because we have save the configuration,so if you reboot the VyOS system but the static router still works.

If you change the configuration,you must commit and save it then you can exit configure mode.But you can use vyos.exit(force=Ture) to execute "exit discard" command. 

##Something you should know
1.Only admin level user can use this library to login and execute all configuration methods.  

2.set() and delete() method is the core function of this library,you can just use a same configuration command on VyOS to set or delete a specify configuration.But you should take a look at the Basic Example and All Methods sections.  

#Quick Start
I will show you how to use this library to configuration the VyOS

The first step is login and configure a interface for management.Look at this example,in this example,I have configure eth0 for vymgmt,and I will show you how to login and configure an interface by this library。

Let's check the interfaces.Well,eth1 is the target interface,and we should set a address for this interface:

	vyos@vyos1:~$ show interfaces
	Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down
	Interface        IP Address                        S/L  Description
	---------        ----------                        ---  -----------
	eth0             192.168.225.2/24                  u/u  
	eth1             -                                 u/u  
	eth2             192.168.83.142/24                 u/u  
	lo               127.0.0.1/8                       u/u  
	                 ::1/128

Let's import the Router and initialize a Router instance:
	
	>>> from vymgmt.router import Router
	>>> vyos1 = Router('192.168.225.2','vyos','vyos')

You should import class Router from vymgmt.router,the use address and "username:password" to initialize the instance.

Then,you can use login() to login vyos1:
	
	>>> vyos1.login()

If there are no problems,this method will return nothing to user.

By now,you can use configure() and execute configuration commands:

	>>> vyos1.configure()

You can use _status() method to get the status of this instance:

	>>> vyos1._status()
	{'session_modified': False, 'logged_in': True, 'session_saved': True, 'conf_mode': True}

When you login,"logged\_in"'s value will change to True.And now,the value of "conf_mode" is True.

Next,we can use set() to set a address for eth1:

	>>> vyos1.set("interfaces ethernet eth1 address '192.168.10.5'")
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/usr/local/lib/python3.5/dist-packages/vymgmt-0.1-py3.5.egg/vymgmt/router.py", line 190, in set
	vymgmt.router.ConfigError:  set interfaces ethernet eth1 address '192.168.10.5'
	
	  Invalid IPv4 address/prefix
  
	  Value validation failed
	  Set failed

	[edit]
	vyos@test-vyos

Oh,NO!I just forgot the netmask.But you can see,if your input has mistakes,this library will raise a exception and display the error message to you.Because my configuration lost netmask,therefore the error reason is invalid address.And vymgmt will raise a ConfigError.You can see "Exceptions" section to get more information.

Now,let's use a correct configuration:

	>>> vyos1.set("interfaces ethernet eth1 address '192.168.10.5/24'")

Well,I have set the address for eth1 now.

Let's check the status now:

	>>> vyos1._status()
	{'session_modified': True, 'logged_in': True, 'session_saved': True, 'conf_mode': True}

You can see,"commit" and "save" are "No",so,you must commit and save it.

Commit:

	>>> vyos1.commit()

Save it:

	>>> vyos1.save()

Let's check the status again:

	>>> vyos1.status()
	{'status': 'login', 'commit': 'Yes', 'save': 'Yes', 'configure': 'Yes'}

You see,the value of "commit" and "save" have changed to "Yes",now I can exit the configure mode and logout.But if you don't commit and save,you still can use "vyos1.exit(force=True)" to exit and discard what you have configured:

	>>> vyos1.exit()
	>>> vyos1._status()
	{'session_modified': False, 'logged_in': True, 'session_saved': True, 'conf_mode': False}
	>>> vyos1.logout()
	>>> vyos1._status()
	{'session_modified': False, 'logged_in': False, 'session_saved': True, 'conf_mode': False}

But once you want to login again,you don't need to create a new instance,just use the former instance:

	>>> vyos1.login()

Now,I have configured an ethernet interface by this library,let's check it on vyos1:

	vyos@vyos1:~$ show interfaces
	Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down
	Interface        IP Address                        S/L  Description
	---------        ----------                        ---  -----------
	eth0             192.168.225.2/24                  u/u  
	eth1             192.168.10.5/24                   u/u  
	eth2             192.168.83.142/24                 u/u  
	lo               127.0.0.1/8                       u/u  
	                 ::1/128

Now,I will show you how to use set() method to configure a rip router:

Topo:

	test1---VyOS1———VyOS2---test3

The address of test1 is 192.168.225.3,and the address of test3 is 192.168.157.8.Now these two vms can not ping each other.

	root@test1:~# ping -c 5 192.168.157.8
	PING 192.168.157.8 (192.168.157.8) 56(84) bytes of data.

	--- 192.168.157.8 ping statistics ---
	5 packets transmitted, 0 received, 100% packet loss, time 4017ms

I have set two test vms' gateway.Now login vyos1,configure the rip network:

	>>> from vymgmt.router import Router
	>>> vyos1 = Router('192.168.225.2','vyos','vyos')
	>>> vyos1.login()
	>>> vyos1.configure()

First,we should add a new lo address:

	>>> vyos1.set("interfaces loopback lo address 1.1.1.1/32")

Then configure rip networks:

	>>> vyos1.set("protocols rip network 192.168.225.0/24")
	>>> vyos1.set("protocols rip network 192.168.10.0/24")

And the last step:

	>>> vyos1.set("protocols rip redistribute connected")

Sometimes,you may forget to commit or save,the library will raise an exception and refuse to exit:

	>>> vyos1._status()
	{'session_modified': True, 'logged_in': True, 'session_saved': True, 'conf_mode': True}
	>>> vyos1.exit()
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/usr/local/lib/python3.5/dist-packages/vymgmt-0.1-py3.5.egg/vymgmt/router.py", line 168, in exit
	vymgmt.router.VyOSError: Cannot exit a session with uncommited changes, use force flag to discard
	>>> vyos1.save()
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/usr/local/lib/python3.5/dist-packages/vymgmt-0.1-py3.5.egg/vymgmt/router.py", line 152, in save
	vymgmt.router.VyOSError: Cannot save when there are uncommited changes

Therefore,we should execute commit() and save():

	>>> vyos1.commit()
	>>> vyos1.save()

Then on vyos2,we can configure rip network:

	>>> vyos2 = Router('192.168.10.6','vyos','vyos')
	>>> vyos2.login()
	>>> vyos2.configure()
	>>> vyos2.set("protocols rip network 192.168.10.0/24")
	>>> vyos2.set("protocols rip network 192.168.157.0/24")
	>>> vyos2.set("protocols rip redistribute connected")
	>>> vyos2.commit()
	>>> vyos2.save()

Then check it on test1:

	root@test1:~# ping 192.168.157.8 -c 5
	PING 192.168.157.8 (192.168.157.8) 56(84) bytes of data.
	64 bytes from 192.168.157.8: icmp_seq=1 ttl=62 time=0.947 ms
	64 bytes from 192.168.157.8: icmp_seq=2 ttl=62 time=1.12 ms
	64 bytes from 192.168.157.8: icmp_seq=3 ttl=62 time=1.34 ms
	64 bytes from 192.168.157.8: icmp_seq=4 ttl=62 time=1.37 ms
	64 bytes from 192.168.157.8: icmp_seq=5 ttl=62 time=1.48 ms

	--- 192.168.157.8 ping statistics ---
	5 packets transmitted, 5 received, 0% packet loss, time 4009ms
	rtt min/avg/max/mdev = 0.947/1.255/1.482/0.196 ms

On test3:

	root@test3:~# ping 192.168.225.3 -c 5
	PING 192.168.225.3 (192.168.225.3) 56(84) bytes of data.
	64 bytes from 192.168.225.3: icmp_seq=1 ttl=62 time=1.18 ms
	64 bytes from 192.168.225.3: icmp_seq=2 ttl=62 time=1.38 ms
	64 bytes from 192.168.225.3: icmp_seq=3 ttl=62 time=1.25 ms
	64 bytes from 192.168.225.3: icmp_seq=4 ttl=62 time=1.35 ms
	64 bytes from 192.168.225.3: icmp_seq=5 ttl=62 time=1.03 ms

	--- 192.168.225.3 ping statistics ---
	5 packets transmitted, 5 received, 0% packet loss, time 4008ms
	rtt min/avg/max/mdev = 1.036/1.241/1.381/0.127 ms

Now,maybe you are understand how to use this library to configure VyOS. 
 
#All Methods
##_status():
Check the router object inner status.  

Return a dictionary include the status of Router instance.  

##login():
Login the VyOS system when you have create a new BasicRouter instance.  

##logout()
Logout the VyOS system.  

This method execute a close() on a connection.  

You can use this method logout a router substance.After logout,you can use the same instance to login again.  

##configure()
Enter the VyOS configure mode  

You must login the VyOS system before use this method.  

##commit()
Commit the configuration changes.  

You can use this method to commit your configuration.  

Due to configuration methods will change the value of the keys in self.__status.You should commit the configurations then can you save or exit.  

##save()
Save the configuration after commit.  

You can use this method to save your configuration.  

##exit(force=False)
Exit VyOS configure mode.  
If you not use "force",you can exit without save but you should commit first or it will raise a exception  

Example:  
force: True or False  

If force=True,this method will execute "exit discard" command and all your changes will lost.  

##set(config)
Basic 'set' method,execute the set command in VyOS.

Example:  
config: "interfaces ethernet eth0 description 'eth0'"

The minimal configuration method.

##delete(config)
Basic 'delete' method,execute the delete command in VyOS.

Example:  
config: "interfaces ethernet eth0 description 'eth0'"

The minimal configuration method.

#Exceptions
##vymgmt.router.VyOSError

This exception class is for most obviously configuration mistakes.  

When this exception raise,the error message from VyOS will displayed.

##vymgmt.router.ConfigError

This exception class is for set/delete failures due to user's wrong input.

When this exception raise,the error message from VyOS will displayed.

##vymgmt.router.CommitError

This exception class is for commit failures and commit conflict.

When this exception raise,the error message from VyOS will displayed.

##vymgmt.router.ConfigLocked

This exception class is for commit() failures due to the commit conflicts when more than one users committing their configurations at the same time.

When this exception raise,the error message from VyOS will displayed.