diff options
author | hochikong <1097225749@qq.com> | 2016-08-18 16:26:43 +0800 |
---|---|---|
committer | hochikong <1097225749@qq.com> | 2016-08-18 16:26:43 +0800 |
commit | 2584bea48c4c243fdf694cb21505bfbde6834a7f (patch) | |
tree | 228a44cb97f93b3972507d9c2054c324da3e9503 | |
parent | 74de52cceec95ee3de95180ee1f1660c75eb5210 (diff) | |
download | python-vyos-mgmt-2584bea48c4c243fdf694cb21505bfbde6834a7f.tar.gz python-vyos-mgmt-2584bea48c4c243fdf694cb21505bfbde6834a7f.zip |
T86 Python management library methods give timeoutsOLD_VERSION
Fix some problem and add BGP support
-rw-r--r-- | setup.py | 2 | ||||
-rw-r--r-- | vyroute/Router.py | 465 | ||||
-rw-r--r-- | vyroute/basic_function/BGPRoute.py | 36 | ||||
-rw-r--r-- | vyroute/basic_function/DeleteRoute.py | 40 | ||||
-rw-r--r-- | vyroute/basic_function/Modifylo.py | 19 | ||||
-rw-r--r-- | vyroute/basic_function/OSPFRoute.py | 116 | ||||
-rw-r--r-- | vyroute/basic_function/RIPRoute.py | 50 | ||||
-rw-r--r-- | vyroute/basic_function/StaticRoute.py | 25 |
8 files changed, 392 insertions, 361 deletions
@@ -5,7 +5,7 @@ setup( name="vyroute", version="0.1", packages=find_packages(), - install_requires=['Exscript'], + install_requires=['pexpect'], description="A library for VyOS routing setting", long_description="A library for VyOS routing setting", diff --git a/vyroute/Router.py b/vyroute/Router.py index f6c3b58..8dc1606 100644 --- a/vyroute/Router.py +++ b/vyroute/Router.py @@ -10,62 +10,7 @@ from vyroute.basic_function import BGPRoute class Router(object): - # Static router configuration func - def static_route(self, data): - pass - - # RIP router configuration func - def rip_route(self, data): - pass - - # OSPF router configuration func - def ospf_area(self, data): - pass - - def router_id(self, data): - pass - - def ospf_redistribute(self, data): - pass - - def ospf_adjacency(self): - pass - - def ospf_default_route(self, data): - pass - - def ospf_route_map(self, data): - pass - - # Interfaces configuration func - def lo(self, data): - pass - - def delete_route(self, data): - pass - - # Basic VyOS configuration func - def status(self): - pass - - def configure(self): - pass - - def commit_config(self): - pass - - def save_config(self): - pass - - def exit_config(self): - pass - - # Login and logout a router - def login(self): - pass - - def logout(self): - pass + pass class BasicRouter(Router): @@ -84,45 +29,44 @@ class BasicRouter(Router): self.__status = {"status": None, "commit": None, "save": None, "configure": None} def status(self): - """Check the router inner status + """Check the router object inner status - :return: a python dictionary + :return: A python dictionary include the status of the router object """ return self.__status def login(self): """Login the router - :return: a python dictionary + :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."} + return "Result : Login successfully." else: - return {"Error": "Connect Failed."} + return "Error : Connect Failed." except Exception as e: - return {"Error": e} + return e def logout(self): """Logout the router - Attention! If you logout,you can not reuse this Router object.You should create a new BasicRouter - :return: a python dictionary + :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."} + return "Result : Logout successfully." except Exception as e: - return {"Error": e} + return e def configure(self): """Enter the VyOS configure mode - :return: a python dictionary + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -131,70 +75,72 @@ class BasicRouter(Router): self.__conn.prompt(0) self.__conn.set_unique_prompt() self.__status["configure"] = "Yes" - return {"Result": "Active configure mode successfully."} + return "Result : Active configure mode successfully." else: - return {"Error": "In configure mode now!"} + return "Error : In configure mode now!" else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def commit_config(self): + def commit(self): """Commit the configuration changes - :return: a python dictionary + :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."} + 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."} + return "Result : Commit successfully." else: - return {"Error": "You have committed!"} + return "Error : You have committed!" else: - return {"Error": "Router not in configure mode!"} + return "Error : Router not in configure mode!" else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def save_config(self): + def save(self): """Save the configuration after commit - :return: a python dictionary + :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."} + return "Error : You don't need to save." if self.__status["save"] == "No": self.__conn.sendline("save") - self.__conn.prompt() + self.__conn.prompt(0) self.__status["save"] = "Yes" - return {"Result": "Save successfully."} + return "Result : Save successfully." else: - return {"Error": "You have saved!"} + 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!"} + return "Error : You need to commit first!" else: - return {"Error": "Router not in configure mode!"} + return "Error : Router not in configure mode!" else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def exit_config(self, force=False): + def exit(self, force=False): """Exit VyOS configure mode :param force: True or False - :return: a python dictionary + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -205,7 +151,7 @@ class BasicRouter(Router): self.__status["configure"] = "No" self.__status["save"] = None self.__status["commit"] = None - return {"Result": "Exit configure mode successfully."} + return "Result : Exit configure mode successfully." else: if self.__status["commit"] == "Yes": if self.__status["save"] == "Yes": @@ -214,37 +160,36 @@ class BasicRouter(Router): self.__status["configure"] = "No" self.__status["save"] = None self.__status["commit"] = None - return {"Result": "Exit configure mode successfully."} + return "Result : Exit configure mode successfully." else: - return {"Error": "You should save first."} + 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."} + return "Result : Exit configure mode successfully." else: - return {"Error": "You should commit first."} + return "Error : You should commit first." else: - return {"Error": "You are not in configure mode,need not exit."} + return "Error : You are not in configure mode,need not exit." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def lo(self, data): - """Modify a router loopback address + def lo(self, lo_address): + """Add a router loopback address - Parameter data example: - {'config':'1.1.1.1/32' - } + Parameter example: + '1.1.1.1/32' - :param data: a python dictionary - :return: a python dictionary + :param lo_address: The target address you want.Don't forget the netmask + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = Modifylo.modifylo(self.__conn, data) + res = Modifylo.modifylo(self.__conn, lo_address) if "Result" in res: if self.__status["commit"] == "No": pass @@ -258,59 +203,67 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} - - def delete_route(self, data): - """Delete router setting - - Parameter data example: - {'config':'rip'/'static'/'ospf'/'all' - } - - :param data: a python dictionary - :return: a python dictionary - """ - try: - if self.__status["status"] == "login": - if self.__status["configure"] == "Yes": - res = DeleteRoute.deleteroute(self.__conn, data) - 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 + return e + + def delete_route(self, route_type): + """Delete router configurations + + Parameter example: + 'rip'/'static'/'ospf'/'bgp'/'all' + + WARNING! + When you use this function,please don't forget this func will delete all same type + router configuration,when your 'config' in data is 'rip',it will delete all rip router setting. + If you do not want your setting disappear,you can delete router configuration manually or rewrite + this func. + + :param route_type: Route type + :return: A message or an error + """ + try: + if self.__status["status"] == "login": + if self.__status["configure"] == "Yes": + res = DeleteRoute.deleteroute(self.__conn, route_type) + if "Result" in res: + if self.__status["commit"] == "No": + pass else: - return res + self.__status["commit"] = "No" + if self.__status["save"] == "No": + pass + else: + self.__status["save"] = "No" + return res else: - return {"Error": "You are not in configure mode."} + return res else: - return {"Error": "Router object not connect to a router."} - except Exception as e: - return {"Error": e} + 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 static_route(self, data): - """Static router setting + def static_route(self, network_range, next_hop, distance): + """This method provide a basic static router configuration function - Parameter data example: - {'config':{'target':'10.20.10.0/24','next-hop':'10.20.10.1','distance':'1'}, - } + Parameter example: + 'network_range':'10.20.10.0/24' + 'next-hop':'10.20.10.1' + 'distance':'1' - :param data: a python dictionary - :return: a python dictionary + :param network_range: The target network,don't forget the netmask + :param next_hop: The next hop + :param distance: The distance + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = StaticRoute.staticroute(self.__conn, data) + res = StaticRoute.staticroute(self.__conn, network_range, next_hop, distance) if "Result" in res: if self.__status["commit"] == "No": pass @@ -324,26 +277,53 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e + + def rip_network(self, network_range): + """RIP router network setting + + Parameter example: + '10.20.10.0/24' - def rip_route(self, data): - """RIP protocols router setting + :param network_range: The target network,don't forget the netmask + :return: A message or an error + """ + try: + if self.__status["status"] == "login": + if self.__status["configure"] == "Yes": + res = RIPRoute.rip_network(self.__conn, network_range) + 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 - Parameter data example: - {'config':'192.168.10.0/24', - } + def rip_redistribute(self): + """Execute 'set protocols rip redistribute connected' command - :param data: a python dictionary - :return: a python dictionary + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = RIPRoute.riproute(self.__conn, data) + res = RIPRoute.rip_redistribute(self.__conn) if "Result" in res: if self.__status["commit"] == "No": pass @@ -357,26 +337,27 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def ospf_area(self, data): - """OSPF area setting + def ospf_area(self, area, network_range): + """This method provide a OSPF area configuration function - Parameter data example: - {'config':{'area':'0','network':'192.168.10.0/24'}, - } + Parameter example: + 'area':'0' + 'network_range':'192.168.10.0/24' - :param data: a python dictionary - :return: a python dictionary + :param area: The ospf area number + :param network_range: The target network,don't forget the netmask + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = OSPFRoute.ospfarea(self.__conn, data) + res = OSPFRoute.ospfarea(self.__conn, area, network_range) if "Result" in res: if self.__status["commit"] == "No": pass @@ -390,26 +371,25 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def router_id(self, data): - """OSPF router id setting + def ospf_router_id(self, router_id): + """This method provide a router id configuration function - Parameter data example: - {'config':{'id':'1.1.1.1'}, - } + Parameter example: + '1.1.1.1' - :param data: a python dictionary - :return: a python dictionary + :param router_id: The router id + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = OSPFRoute.router_id(self.__conn, data) + res = OSPFRoute.ospf_router_id(self.__conn, router_id) if "Result" in res: if self.__status["commit"] == "No": pass @@ -423,26 +403,25 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def ospf_redistribute(self, data): + def ospf_redistribute(self, metric_type): """OSPF redistribute setting - Parameter data example: - {'config':{'type':'2'}, - } + Parameter example: + '2' - :param data: a python dictionary - :return: a python dictionary + :param metric_type: The metric-type + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = OSPFRoute.ospf_redistribute(self.__conn, data) + res = OSPFRoute.ospf_redistribute(self.__conn, metric_type) if "Result" in res: if self.__status["commit"] == "No": pass @@ -456,16 +435,16 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e def ospf_adjacency(self): - """set protocols ospf log-adjacency-changes + """Execute 'Set protocols ospf log-adjacency-changes' command - :return: a python dictionary + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -484,26 +463,27 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def ospf_default_route(self, data): - """set protocols ospf default-information originate always (and other 2 commands) + def ospf_default_route(self, metric, metric_type): + """This method execute the commands to configure default route - Parameter data example: - {'config':{'metric':'10','metric-type':'2'}, - } + Parameter example: + 'metric':'10' + 'metric-type':'2' - :param data: a python dictionary - :return: a python dictionary + :param metric: The metric,a number + :param metric_type: The metric-type + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = OSPFRoute.ospf_default_route(self.__conn, data) + res = OSPFRoute.ospf_default_route(self.__conn, metric, metric_type) if "Result" in res: if self.__status["commit"] == "No": pass @@ -517,26 +497,27 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def ospf_route_map(self, data): + def ospf_route_map(self, rule, interface): """VyOS route-map setting when you configure a OSPF router - Parameter data example: - {'config':{'rule':'10','interface':'lo'}, - } + Parameter example: + 'rule':'10' + 'interface':'lo' - :param data: a python dictionary - :return: a python dictionary + :param rule: The route-map rule number + :param interface: The interface name + :return: A message or an error """ try: if self.__status["status"] == "login": if self.__status["configure"] == "Yes": - res = OSPFRoute.ospf_route_map(self.__conn, data) + res = OSPFRoute.ospf_route_map(self.__conn, rule, interface) if "Result" in res: if self.__status["commit"] == "No": pass @@ -550,21 +531,28 @@ class BasicRouter(Router): else: return res else: - return {"Error": "You are not in configure mode."} + return "Error : You are not in configure mode." else: - return {"Error": "Router object not connect to a router."} + return "Error : Router object not connect to a router." except Exception as e: - return {"Error": e} + return e - def bgp_as(self, self_as, neighbor, multihop, remote_as, update_source): + def bgp_route(self, self_as, neighbor, multihop, remote_as, update_source): """VyOS BGP router basic setting + Parameter example: + 'self_as':'65538' + 'neighbor':'192.168.10.5' + 'multihop':'2' + 'remote_as':'65537' + 'update_source':'192.168.10.6' + :param self_as: The AS number of the router you login :param neighbor: The neighbor router address :param multihop: The amount of hops :param remote_as: The remote AS number :param update_source: The update source - :return: A message + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -583,18 +571,22 @@ class BasicRouter(Router): else: return res else: - return "Error:You are not in configure mode." + return "Error : You are not in configure mode." else: - return "Error:Router object not connect to a router." + return "Error : Router object not connect to a router." except Exception as e: return e def bgp_network(self, self_as, network_range): """Add a network to BGP router + Parameter example: + 'self_as':'65538' + 'network_range':'10.20.10.0/24' + :param self_as: The AS number of the router you login :param network_range: The target network,don't forget the netmask - :return: A message + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -613,18 +605,22 @@ class BasicRouter(Router): else: return res else: - return "Error:You are not in configure mode." + return "Error : You are not in configure mode." else: - return "Error:Router object not connect to a router." + return "Error : Router object not connect to a router." except Exception as e: return e def bgp_router_id(self, self_as, router_id): """Set a router id for the router you login + Parameter example: + 'self_as':'65538' + 'router_id':'10.20.10.0' + :param self_as: The AS number of the router you login :param router_id: The router id,or you can use the router address as router id - :return: A message + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -643,17 +639,20 @@ class BasicRouter(Router): else: return res else: - return "Error:You are not in configure mode." + return "Error : You are not in configure mode." else: - return "Error:Router object not connect to a router." + return "Error : Router object not connect to a router." except Exception as e: return e def bgp_blackhole_route(self, network_range): """Set a blackhole route + Parameter example: + '10.20.10.0/24' + :param network_range: The target network,don't forget the netmask - :return: A message + :return: A message or an error """ try: if self.__status["status"] == "login": @@ -672,8 +671,8 @@ class BasicRouter(Router): else: return res else: - return "Error:You are not in configure mode." + return "Error : You are not in configure mode." else: - return "Error:Router object not connect to a router." + return "Error : Router object not connect to a router." except Exception as e: return e diff --git a/vyroute/basic_function/BGPRoute.py b/vyroute/basic_function/BGPRoute.py index bcb891d..a84c896 100644 --- a/vyroute/basic_function/BGPRoute.py +++ b/vyroute/basic_function/BGPRoute.py @@ -2,13 +2,20 @@ def bgp_as(obj, self_as, neighbor, multihop, remote_as, update_source): """VyOS BGP router basic setting about AS + Parameter example: + 'self_as':'65538' + 'neighbor':'192.168.10.5' + 'multihop':'2' + 'remote_as':'65537' + 'update_source':'192.168.10.6' + :param obj: A connection object :param self_as: The AS number of the router you login :param neighbor: The neighbor router address :param multihop: The amount of hops :param remote_as: The remote AS number :param update_source: The update source - :return: A message + :return: A message or an error """ bgp_multihop_configuration = "set protocols bgp %s neighbor %s ebgp-multihop %s" bgp_remote_as_configuration = "set protocols bgp %s neighbor %s remote-as %s" @@ -35,7 +42,7 @@ def bgp_as(obj, self_as, neighbor, multihop, remote_as, update_source): if reg > 0: return error_message else: - return "Result:Configured successfully" + return "Result : Configured successfully" except Exception as e: return e @@ -43,10 +50,14 @@ def bgp_as(obj, self_as, neighbor, multihop, remote_as, update_source): def bgp_network(obj, self_as, network_range): """Add a network to BGP router + Parameter example: + 'self_as':'65538' + 'network_range':'10.20.10.0/24' + :param obj: A connection object :param self_as: The AS number of the router you login :param network_range: The target network,don't forget the netmask - :return: A message + :return: A message or an error """ bgp_network_configuration = "set protocols bgp %s network %s" @@ -56,7 +67,7 @@ def bgp_network(obj, self_as, network_range): if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return "Result:Configured successfully" + return "Result : Configured successfully" except Exception as e: return e @@ -64,10 +75,14 @@ def bgp_network(obj, self_as, network_range): def bgp_router_id(obj, self_as, router_id): """Set a router id for the router you login + Parameter example: + 'self_as':'65538' + 'router_id':'10.20.10.0' + :param obj:A connection object :param self_as: The AS number of the router you login :param router_id: The router id,or you can use the router address as router id - :return: A message + :return: A message or an error """ bgp_router_id_configuration = "set protocols bgp %s parameters router-id %s" @@ -77,7 +92,7 @@ def bgp_router_id(obj, self_as, router_id): if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return "Result:Configured successfully" + return "Result : Configured successfully" except Exception as e: return e @@ -85,11 +100,14 @@ def bgp_router_id(obj, self_as, router_id): def bgp_blackhole_route(obj, network_range): """Set a blackhole route + Parameter example: + '10.20.10.0/24' + :param obj: A connection object :param network_range: The target network,don't forget the netmask - :return: A message + :return: A message or an error """ - bgp_blackhole_configuration = "set protocols static route %s blackhole distance '254' " + bgp_blackhole_configuration = "set protocols static route %s blackhole distance 254" try: obj.sendline(bgp_blackhole_configuration % network_range) @@ -97,6 +115,6 @@ def bgp_blackhole_route(obj, network_range): if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return "Result:Configured successfully" + return "Result : Configured successfully" except Exception as e: return e diff --git a/vyroute/basic_function/DeleteRoute.py b/vyroute/basic_function/DeleteRoute.py index 048406c..175b712 100644 --- a/vyroute/basic_function/DeleteRoute.py +++ b/vyroute/basic_function/DeleteRoute.py @@ -1,10 +1,9 @@ # Copyright (c) 2016 Hochikong -def deleteroute(obj, data): +def deleteroute(obj, route_type): """This method provide a router configuration delete function - Parameter data example: - {'config':'rip'/'static'/'ospf'/'all' - } + Parameter example: + 'rip'/'static'/'ospf'/'bgp'/'all' WARNING! When you use this function,please don't forget this func will delete all same type @@ -12,43 +11,50 @@ def deleteroute(obj, data): If you do not want your setting disappear,you can delete router configuration manually or rewrite this func. - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param route_type: Route type + :return: A message or an error """ delete_basic_configuration = "delete protocols %s" delete_all_protocols = "delete protocols" try: - if data['config'] == "all": + if route_type == "all": obj.sendline(delete_all_protocols) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Delete successfully."} - elif data['config'] == 'rip': + return "Result : Delete successfully." + elif route_type == 'rip': obj.sendline(delete_basic_configuration % 'rip') obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Delete successfully."} - elif data['config'] == 'static': + return "Result : Delete successfully." + elif route_type == 'static': obj.sendline(delete_basic_configuration % 'static') obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Delete successfully."} - elif data['config'] == 'ospf': + return "Result : Delete successfully." + elif route_type == 'bgp': + obj.sendline(delete_basic_configuration % 'bgp') + obj.prompt() + if len(obj.before) > obj.before.index('\r\n') + 2: + return obj.before + else: + return "Result : Delete successfully." + elif route_type == 'ospf': obj.sendline(delete_basic_configuration % 'ospf') obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Delete successfully."} + return "Result : Delete successfully." else: - return {"Error": "Nonsupport protocols type."} + return "Error : Nonsupport protocols type." except Exception as e: - return {"Error": e} + return e diff --git a/vyroute/basic_function/Modifylo.py b/vyroute/basic_function/Modifylo.py index 5dca08f..ee247f0 100644 --- a/vyroute/basic_function/Modifylo.py +++ b/vyroute/basic_function/Modifylo.py @@ -1,25 +1,24 @@ # Copyright (c) 2016 Hochikong -def modifylo(obj, data): +def modifylo(obj, lo_address): """This method provide a loopback address configuration function - Parameter data example: - {'config':'1.1.1.1/32' - } + Parameter example: + '1.1.1.1/32' - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param lo_address: The target address you want.Don't forget the netmask + :return: A message or an error """ lo_basic_configuration = "set interfaces loopback lo address %s" try: # Configure loopback interface lo address - obj.sendline(lo_basic_configuration % data['config']) + obj.sendline(lo_basic_configuration % lo_address) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Modify successfully."} + return "Result : Add successfully." except Exception as e: - return {'Error': e} + return e diff --git a/vyroute/basic_function/OSPFRoute.py b/vyroute/basic_function/OSPFRoute.py index b7c1c40..4f80b88 100644 --- a/vyroute/basic_function/OSPFRoute.py +++ b/vyroute/basic_function/OSPFRoute.py @@ -1,63 +1,62 @@ # Copyright (c) 2016 Hochikong -def ospfarea(obj, data): +def ospfarea(obj, area, network_range): """This method provide a OSPF area configuration function - Parameter data example: - {'config':{'area':'0','network':'192.168.10.0/24'}, - } + Parameter example: + 'area':'0' + 'network_range':'192.168.10.0/24' - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param area: The ospf area number + :param network_range: The target network,don't forget the netmask + :return: A message or an error """ ospf_basic_configuration = "set protocols ospf area %s network %s" try: # Configure ospf area - obj.sendline(ospf_basic_configuration % (data['config']['area'], data['config']['network'])) + obj.sendline(ospf_basic_configuration % (area, network_range)) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e -def router_id(obj, data): +def ospf_router_id(obj, router_id): """This method provide a router id configuration function - Parameter data example: - {'config':{'id':'1.1.1.1'}, - } + Parameter example: + '1.1.1.1' :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param router_id: The router id + :return: A message or an error """ router_id_configuration = "set protocols ospf parameters router-id %s" try: # Configure router id - obj.sendline(router_id_configuration % data['config']['id']) + obj.sendline(router_id_configuration % router_id) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e -def ospf_redistribute(obj, data): +def ospf_redistribute(obj, metric_type): """This method provide a router redistribute function - Parameter data example: - {'config':{'type':'2'}, - } + Parameter example: + '2' - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param metric_type: The metric-type + :return: A message or an error """ redistribute_configuration = {"0": "set protocols ospf redistribute connected metric-type %s", "1": "set protocols ospf redistribute connected route-map CONNECT", @@ -65,7 +64,7 @@ def ospf_redistribute(obj, data): try: reg = 0 error_message = [] - obj.sendline(redistribute_configuration['0'] % data['config']['type']) + obj.sendline(redistribute_configuration['0'] % metric_type) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: error_message.append(obj.before) @@ -78,16 +77,16 @@ def ospf_redistribute(obj, data): if reg > 0: return error_message else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e def ospf_adjacency(obj): - """This method sendline : set protocols ospf log-adjacency-changes + """This method execute : set protocols ospf log-adjacency-changes :param obj: a connection object - :return: a python dictionary + :return: A message or an error """ log_adjacency_changes_configuration = "set protocols ospf log-adjacency-changes" try: @@ -96,22 +95,22 @@ def ospf_adjacency(obj): if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e -def ospf_default_route(obj, data): - """This method sendline : set protocols ospf default-information originate always - and other commands +def ospf_default_route(obj, metric, metric_type): + """This method execute the commands to configure default route - Parameter data example: - {'config':{'metric':'10','metric-type':'2'}, - } + Parameter example: + 'metric':'10' + 'metric-type':'2' - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param metric: The metric,a number + :param metric_type: The metric-type + :return: A message or an error """ default_route_configuration = {"0": "set protocols ospf default-information originate always", "1": "set protocols ospf default-information originate metric %s", @@ -125,12 +124,12 @@ def ospf_default_route(obj, data): if len(obj.before) > obj.before.index('\r\n') + 2: error_messsage.append(obj.before) reg += 1 - obj.sendline(default_route_configuration['1'] % data['config']['metric']) + obj.sendline(default_route_configuration['1'] % metric) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: error_messsage.append(obj.before) reg += 1 - obj.sendline(default_route_configuration['2'] % data['config']['metric-type']) + obj.sendline(default_route_configuration['2'] % metric_type) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: error_messsage.append(obj.before) @@ -138,21 +137,22 @@ def ospf_default_route(obj, data): if reg > 0: return error_messsage else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e -def ospf_route_map(obj, data): - """This method used for VyOS route-map setting when you configure a OSPF router +def ospf_route_map(obj, rule, interface): + """This method is used for VyOS route-map setting when you configure a OSPF router - Parameter data example: - {'config':{'rule':'10','interface':'lo'}, - } + Parameter example: + 'rule':'10' + 'interface':'lo' - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param rule: The route-map rule number + :param interface: The interface name + :return: A message or an error """ route_map_configuration = {"0": "set policy route-map CONNECT rule %s action permit", "1": "set policy route-map CONNECT rule %s match interface %s", @@ -160,12 +160,12 @@ def ospf_route_map(obj, data): try: reg = 0 error_messsage = [] - obj.sendline(route_map_configuration['0'] % data['config']['rule']) + obj.sendline(route_map_configuration['0'] % rule) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: error_messsage.append(obj.before) reg += 1 - obj.sendline(route_map_configuration['1'] % (data['config']['rule'], data['config']['interface'])) + obj.sendline(route_map_configuration['1'] % (rule, interface)) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: error_messsage.append(obj.before) @@ -173,6 +173,6 @@ def ospf_route_map(obj, data): if reg > 0: return error_messsage else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e diff --git a/vyroute/basic_function/RIPRoute.py b/vyroute/basic_function/RIPRoute.py index cb421e0..632dc6d 100644 --- a/vyroute/basic_function/RIPRoute.py +++ b/vyroute/basic_function/RIPRoute.py @@ -1,34 +1,42 @@ # Copyright (c) 2016 Hochikong -def riproute(obj, data): - """This method provide a RIP protocols router configuration function +def rip_network(obj, network_range): + """This method provide a RIP router network configuration function - Parameter data example: - {'config':'192.168.10.0/24', - } + Parameter example: + '10.20.10.0/24' - :param obj: a connection object - :param data: a python dictionary - :return: a python dictionary + :param obj: A connection object + :param network_range: The target network,don't forget the netmask + :return: A message or an error """ rip_basic_configuration = "set protocols rip network %s" - redistribute_configuration = "set protocols rip redistribute connected" + try: # Configure RIP router - reg = 0 - error_messsage = [] - obj.execute(rip_basic_configuration % data['config']) + obj.sendline(rip_basic_configuration % network_range) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: - error_messsage.append(obj.before) - reg += 1 - obj.execute(redistribute_configuration) + return obj.before + else: + return "Result : Configured successfully" + except Exception as e: + return e + + +def rip_redistribute(obj): + """Execute 'set protocols rip redistribute connected' command + + :param obj: A connection object + :return: A message or an error + """ + redistribute_configuration = "set protocols rip redistribute connected" + + try: + obj.sendline(redistribute_configuration) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: - error_messsage.append(obj.before) - reg += 1 - if reg > 0: - return error_messsage + return obj.before else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {"Error": e} + return e diff --git a/vyroute/basic_function/StaticRoute.py b/vyroute/basic_function/StaticRoute.py index 9bdf378..d210785 100644 --- a/vyroute/basic_function/StaticRoute.py +++ b/vyroute/basic_function/StaticRoute.py @@ -1,26 +1,27 @@ # Copyright (c) 2016 Hochikong -def staticroute(obj, data): +def staticroute(obj, network_range, next_hop, distance): """This method provide a basic static router configuration function - Parameter data example: - {'config':{'target':'10.20.10.0/24','next-hop':'10.20.10.1','distance':'1'}, - } + Parameter example: + 'network_range':'10.20.10.0/24' + 'next-hop':'10.20.10.1' + 'distance':'1' - :param obj: a connection object - :param data: a python dictionary - :return:a python dictionary + :param obj: A connection object + :param network_range: The target network,don't forget the netmask + :param next_hop: The next hop + :param distance: The distance + :return: A message or an error """ static_basic_configuration = "set protocols static route %s next-hop %s distance %s" try: # Configure static router - obj.sendline(static_basic_configuration % (data['config']['target'], - data['config']['next-hop'], - data['config']['distance'])) + obj.sendline(static_basic_configuration % (network_range, next_hop, distance)) obj.prompt() if len(obj.before) > obj.before.index('\r\n') + 2: return obj.before else: - return {"Result": "Configured successfully"} + return "Result : Configured successfully" except Exception as e: - return {'Error': e} + return e |