forked from dggreenbaum/debinterface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.py
executable file
·183 lines (159 loc) · 5.06 KB
/
adapter.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
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
#! /usr/bin/python
# A representation a network adapter.
class networkAdapter:
#Validate an IP Address
# Will return 0 on fail, 1 on success.
# Works for subnet masks too.
def validateIP(self, ip):
import socket
try:
socket.inet_aton(ip)
except socket.error:
return 0
return 1
# Set the inet option of an interface.
def setName(self, n):
self.ifAttributes['name'] = n
# Set the inet option of an interface.
def setInet(self, i):
self.ifAttributes['inet'] = i
# Set the address source for an interface.
def setAddressSource(self, s):
self.ifAttributes['source'] = s
# Set the ipaddress of an interface.
def setAddress(self, a):
if self.validateIP(a) == 1:
self.ifAttributes['address'] = a
else:
pass
# Set the netmask of an interface.
def setNetmask(self, m):
if self.validateIP(m) == 1:
self.ifAttributes['netmask'] = m
else:
pass
# Set the default gateway of an interface.
def setGateway(self, g):
if self.validateIP(g) == 1:
self.ifAttributes['gateway'] = g
else:
pass
# Set the broadcast address of an interface.
def setBroadcast(self, b):
if self.validateIP(b) == 1:
self.ifAttributes['broadcast'] = b
else:
pass
# Set the network identifier of an interface.
def setNetwork(self, w):
if self.validateIP(w) == 1:
self.ifAttributes['network'] = w
else:
pass
# Set the option to autostart the interface.
def setAuto(self, t):
self.ifAttributes['auto'] = t
# Set the option to allow hotplug on the interface.
def setHotplug(self, h):
self.ifAttributes['hotplug'] = h
# Set or append the bridge options of an interface.
# This should be a dictionary mapping option names and values.
# In the interfaces file, options will have a 'bridge_' prefix.
def setBropts(self, opts):
self.ifAttributes['bridge-opts'] = opts
def replaceBropt(self, key, value):
self.ifAttributes['bridge-opts'][key] = value
def appendBropts(self, key, value):
self.ifAttributes['bridge-opts'][key] = self.ifAttributes['bridge-opts'][key] + value
# Set and add to the up commands for an interface.
# Takes a LIST of shell commands.
def setUp(self, up):
self.ifAttributes['up'] = up
def appendUp(self, cmd):
self.ifAttributes['up'].append(cmd)
# Set and add to the down commands for an interface.
# Takes a LIST of shell commands.
def setDown(self, down):
self.ifAttributes['down'] = down
def appendDown(self, cmd):
self.ifAttributes['down'].append(cmd)
# Set and add to the pre-up commands for an interface.
# Takes a LIST of shell commands.
def setPreUp(self, pre):
self.ifAttributes['pre-up'] = pre
def appendPreUp(self, cmd):
self.ifAttributes['pre-up'].append(cmd)
# Set and add to the post-down commands for an interface.
# Takes a LIST of shell commands.
def setPostDown(self, post):
self.ifAttributes['post-down'] = post
def appendPostDown(self, cmd):
self.ifAttributes['post-down'].append(cmd)
# Return the ifAttributes data structure.
def export(self):
return self.ifAttributes
# Display a (kind of) human readable representation of the adapter.
def display(self):
print('============')
for key in self.ifAttributes.keys():
if isinstance(self.ifAttributes[key], list):
print(key + ': ')
for item in self.ifAttributes[key]:
print('\t' + item)
elif isinstance(self.ifAttributes[key], dict):
print(key + ': ')
for item in self.ifAttributes[key].keys():
print('\t' + item + ': ' + self.ifAttributes[key][item])
else:
print(key + ': ' +str(self.ifAttributes[key]))
print('============')
# Set up the network adapter.
def __init__(self, options=None):
# Initialize attribute storage structre.
self.ifAttributes = {}
self.ifAttributes['bridge-opts'] = {}
self.ifAttributes['up'] = []
self.ifAttributes['down'] = []
self.ifAttributes['pre-up'] = []
self.ifAttributes['post-down'] = []
# Set the name of the interface.
if isinstance(options, str):
self.setName(options)
# If a dictionary of options is provided, populate the adapter options.
elif isinstance(options, dict):
for key in options.keys():
if key == 'name':
self.setName(options[key])
if key == 'inet':
self.setInet(options[key])
elif key == 'source':
self.setAddressSource(options[key])
elif key == 'address':
self.setAddress(options[key])
elif key == 'netmask':
self.setNetmask(options[key])
elif key == 'gateway':
self.setGateway(options[key])
elif key == 'broadcast':
self.setBroadcast(options[key])
elif key == 'network':
self.setNetwork(options[key])
elif key == 'auto':
self.setAuto(options[key])
elif key == 'allow-hotplug':
self.setAuto(options[key])
elif key == 'bridgeOpts':
self.setBropts(options[key])
elif key == 'up':
self.setUp(options[key])
elif key == 'down':
self.setDown(options[key])
elif key == 'pre-up':
self.setPreUp(options[key])
elif key == 'post-down':
self.setPostDown(options[key])
# Don't let one stupid option ruin everyone's day.
else:
pass
else:
print("No arguments given. Provide a name or options dict.")