summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhochikong <1097225749@qq.com>2016-08-26 22:04:49 +0800
committerhochikong <1097225749@qq.com>2016-08-26 22:04:49 +0800
commitb6d33086cfecf091a1d4f2afc4bbf4607e4a327c (patch)
treea2bcea05297d6eba7dcaa312302ff77ace2c9150
parenta9d1a18af94f3e0f0ac669c51f3acfb1c03058f0 (diff)
downloadpython-vyos-mgmt-b6d33086cfecf091a1d4f2afc4bbf4607e4a327c.tar.gz
python-vyos-mgmt-b6d33086cfecf091a1d4f2afc4bbf4607e4a327c.zip
Fix some logic error in logout()
Fix two mistakes may raise error exceptions in error_distinguish Add a quick start section to README
-rwxr-xr-xREADME.md224
-rw-r--r--vymgmt/error_distinguish.py5
-rw-r--r--vymgmt/router.py9
3 files changed, 236 insertions, 2 deletions
diff --git a/README.md b/README.md
index 137bc40..f697cbc 100755
--- a/README.md
+++ b/README.md
@@ -50,6 +50,230 @@ If you change the configuration,you must commit and save it then you can exit co
2.When you initialize the Router class,the second parameters is 'username:password'.
3.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()
+ 'Result : Login successfully.'
+
+If there are no problems,this method will return a string message to user.
+
+By now,you can use configure() and execute configuration commands:
+
+ >>> vyos1.configure()
+ 'Result : Active configure mode successfully.'
+
+You can use status() method to get the status of this instance:
+
+ >>> vyos1.status()
+ {'status': 'login', 'commit': None, 'save': None, 'configure': 'Yes'}
+
+When you just initialize,all values are None.When you login,"status"'s value will change to "login"
+
+But you can see "commit" and "save"'s value are None,because now we just login and enter configure mode.Now,the value of "configure" is "Yes".
+
+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 "build/bdist.linux-x86_64/egg/vymgmt/router.py", line 218, in set
+ vymgmt.base_exception.exceptions_for_set_and_delete.ConfigValueError:
+ set interfaces ethernet eth1 address '192.168.10.5'
+
+ Invalid IPv4 address/prefix
+
+ Value validation failed
+ Set failed
+
+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 ConfigValueError.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'")
+ 'Result : Configured successfully'
+
+Well,I set address for eth1 now.
+
+Let's check the status now:
+
+ >>> vyos1.status()
+ {'status': 'login', 'commit': 'No', 'save': 'No', 'configure': 'Yes'}
+
+You can see,"commit" and "save" are "No",so,you must commit and save it.
+
+Commit:
+
+ >>> vyos1.commit()
+ 'Result : Commit successfully.'
+
+Save it:
+
+ >>> vyos1.save()
+ 'Result : Save successfully.'
+
+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()
+ 'Result : Exit configure mode successfully.'
+ >>> vyos1.logout()
+ 'Result : Logout successfully.'
+ >>> vyos1.status()
+ {'status': 'logout', 'commit': None, 'save': None, 'configure': None}
+
+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()
+ 'Result : Login successfully.'
+ >>> vyos1.configure()
+ 'Result : Active configure mode successfully.'
+
+First,we should add a new lo address:
+
+ >>> vyos1.set("interfaces loopback lo address 1.1.1.1/32")
+ 'Result : Configured successfully'
+
+Then configure rip networks:
+
+ >>> vyos1.set("protocols rip network 192.168.225.0/24")
+ 'Result : Configured successfully'
+ >>> vyos1.set("protocols rip network 192.168.10.0/24")
+ 'Result : Configured successfully'
+
+And the last step:
+
+ >>> vyos1.set("protocols rip redistribute connected")
+ 'Result : Configured successfully'
+
+Sometimes,you may forget to commit or save,the library will return a message and refuse to exit:
+
+ >>> vyos1.status()
+ {'status': 'login', 'commit': 'No', 'save': 'No', 'configure': 'Yes'}
+ >>> vyos1.exit()
+ 'Error : You should commit first.'
+ >>> vyos1.save()
+ 'Error : You need to commit first!'
+
+Therefore,we should execute commit() and save():
+
+ >>> vyos1.commit()
+ 'Result : Commit successfully.'
+ >>> vyos1.save()
+ 'Result : Save successfully.'
+
+Then on vyos2,we can configure rip network:
+
+ >>> vyos2 = Router('192.168.10.6','vyos:vyos')
+ >>> vyos2.login()
+ 'Result : Login successfully.'
+ >>> vyos2.configure()
+ 'Result : Active configure mode successfully.'
+ >>> vyos2.set("protocols rip network 192.168.10.0/24")
+ 'Result : Configured successfully'
+ >>> vyos2.set("protocols rip network 192.168.157.0/24")
+ 'Result : Configured successfully'
+ >>> vyos2.set("protocols rip redistribute connected")
+ 'Result : Configured successfully'
+ >>> vyos2.commit()
+ 'Result : Commit successfully.'
+ >>> vyos2.save()
+ 'Result : Save successfully.'
+
+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():
diff --git a/vymgmt/error_distinguish.py b/vymgmt/error_distinguish.py
index d58db2f..1f889f8 100644
--- a/vymgmt/error_distinguish.py
+++ b/vymgmt/error_distinguish.py
@@ -7,7 +7,7 @@ def distinguish_for_set(message):
:param message: A error message string from VyOS
:return: The type of error
"""
- path_error_string = ['Configuration path:', 'is not valid']
+ path_error_string = ['Configuration path:', 'is not valid', 'already exists']
value_error_string = ['Value validation failed']
all_strings = [path_error_string, value_error_string]
condition = 0
@@ -31,7 +31,8 @@ def distinguish_for_delete(message):
:return: The type of error
"""
path_error_string = ['Configuration path:', 'is not valid', 'Delete failed']
- value_error_string = ['Nothing to delete', 'the specified value does not exist']
+ value_error_string = ['Nothing to delete', 'the specified value does not exist',
+ "the specified node does not exist"]
all_strings = [path_error_string, value_error_string]
condition = 0
diff --git a/vymgmt/router.py b/vymgmt/router.py
index f46955d..15fea9e 100644
--- a/vymgmt/router.py
+++ b/vymgmt/router.py
@@ -52,6 +52,15 @@ class Router(object):
:return: A message or an error
"""
try:
+ if self.__status["commit"] == "No":
+ return "Error : You should commit and exit configure mode first."
+
+ if self.__status["save"] == "No":
+ return "Error : You should save and exit configure mode first."
+
+ if self.__status["configure"] == "Yes":
+ return "Error : You should exit configure mode first."
+
self.__conn.close()
self.__status["status"] = "logout"
self.__status["configure"] = None