diff options
Diffstat (limited to 'vyroute/basic_function')
-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 |
6 files changed, 159 insertions, 127 deletions
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 |