-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.py
107 lines (82 loc) · 2.35 KB
/
route.py
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
#
# $Id: route.py 9 2012-06-05 04:56:36Z nickw $
#
# NAME: route.py
#
# AUTHOR: Nick Whalen <[email protected]>
# COPYRIGHT: 2012 by Nick Whalen
# LICENSE:
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DESCRIPTION:
# Defines a route and methods to work with it. Currently only works on Linux; Windows functionality is
# planned.
#
from lib import nixcommon
# Exceptions
class RouteError(Exception):
pass
TYPE = ('unicast', 'local', 'broadcast', 'multicast', 'throw', 'unreachable', 'prohibit', 'blackhole', 'nat')
SCOPE = ('host', 'link', 'global', "%d")
# Route
class Route(object):
"""
Defines a network route and provides methods to work with it.
"""
network = None
nexthop = None
source = None
device = None
description = None
options = []
def __init__(self, route = None):
"""
Constructor
"""
if route:
self.route = route
#---
def __str__(self):
"""
Converts route to iproute2 string.
"""
#---
def _iproute(self, arguments):
"""
Wrapper for calls to 'ip route'.
"""
return nixcommon.runProcess("ip route %s" %arguments)
#---
def validate(self):
"""
Validates a route based on it's network definition.
"""
#---
def apply(self):
"""
Applies the route to the appropriate table.
"""
if not self.route:
raise RouteError('Invalid routing entry (blank).')
self.validate()
route_cfg = "add %s" %self.network
if self.device: route_cfg += "device %s" %self.device
if self.nexthop: route_cfg += "via %s" %self.nexthop
self._iproute(route_cfg)
#---
def parse(self):
"""
Parses a routing string from iproute2.
"""
#---
#---