-
Notifications
You must be signed in to change notification settings - Fork 49
/
interfacesReader.py
143 lines (121 loc) · 5.12 KB
/
interfacesReader.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
# A class representing the contents of /etc/network/interfaces
from adapter import NetworkAdapter
import StringIO
class InterfacesReader:
''' Short lived class to read interfaces file '''
def __init__(self, interfaces_path):
self._interfaces_path = interfaces_path
self._reset()
@property
def adapters(self):
return self._adapters
def parse_interfaces(self):
''' Read /etc/network/interfaces. '''
self._reset()
# Open up the interfaces file. Read only.
with open(self._interfaces_path, "r") as interfaces:
self._read_lines_from_file(interfaces)
return self._parse_interfaces_impl()
def parse_interfaces_from_string(self, data):
self._reset()
# Can't be used in 'with..as'
string_file = StringIO.StringIO(data)
self._read_lines_from_file(string_file)
string_file.close()
return self._parse_interfaces_impl()
def _parse_interfaces_impl(self):
''' Save adapters
Return an array of networkAdapter instances.
'''
for entry in self._auto_list:
for adapter in self._adapters:
if adapter._ifAttributes['name'] == entry:
adapter.setAuto(True)
for entry in self._hotplug_list:
for adapter in self._adapters:
if adapter._ifAttributes['name'] == entry:
adapter.setHotplug(True)
return self._adapters
def _read_lines_from_file(self, fileObj):
# Loop through the interfaces file.
for line in fileObj:
# Identify the clauses by analyzing the first word of each line.
# Go to the next line if the current line is a comment.
if line.strip().startswith("#") is True:
pass
else:
self._parse_iface(line)
# Ignore blank lines.
if line.isspace() is True:
pass
else:
self._parse_details(line)
self._read_auto(line)
self._read_hotplug(line)
def _parse_iface(self, line):
if line.startswith('iface'):
sline = line.split()
# Update the self._context when an iface clause is encountered.
self._context += 1
self._adapters.append(NetworkAdapter(sline[1]))
self._adapters[self._context].setAddressSource(sline[-1])
self._adapters[self._context].setAddrFam(sline[2])
def _parse_details(self, line):
if line[0].isspace() is True:
sline = line.split()
if sline[0] == 'address':
self._adapters[self._context].setAddress(sline[1])
elif sline[0] == 'netmask':
self._adapters[self._context].setNetmask(sline[1])
elif sline[0] == 'gateway':
self._adapters[self._context].setGateway(sline[1])
elif sline[0] == 'broadcast':
self._adapters[self._context].setBroadcast(sline[1])
elif sline[0] == 'network':
self._adapters[self._context].setNetwork(sline[1])
elif sline[0].startswith('bridge') is True:
opt = sline[0].split('_')
sline.pop(0)
ifs = " ".join(sline)
self._adapters[self._context].replaceBropt(opt[1], ifs)
elif sline[0] == 'up' or sline[0] == 'down' or sline[0] == 'pre-up' or sline[0] == 'post-down':
ud = sline.pop(0)
cmd = ' '.join(sline)
if ud == 'up':
self._adapters[self._context].appendUp(cmd)
elif ud == 'down':
self._adapters[self._context].appendDown(cmd)
elif ud == 'pre-up':
self._adapters[self._context].appendPreUp(cmd)
elif ud == 'post-down':
self._adapters[self._context].appendPostDown(cmd)
else:
# store as if so as not to loose it
self._adapters[self._context].setUnknown(sline[0], sline[1])
def _read_auto(self, line):
''' Identify which adapters are flagged auto. '''
if line.startswith('auto'):
sline = line.split()
for word in sline:
if word == 'auto':
pass
else:
self._auto_list.append(word)
def _read_hotplug(self, line):
''' Identify which adapters are flagged allow-hotplug. '''
if line.startswith('allow-hotplug'):
sline = line.split()
for word in sline:
if word == 'allow-hotplug':
pass
else:
self._hotplug_list.append(word)
def _reset(self):
# Initialize a place to store created networkAdapter objects.
self._adapters = []
# Keep a list of adapters that have the auto or allow-hotplug flags set.
self._auto_list = []
self._hotplug_list = []
# Store the interface context.
# This is the index of the adapters collection.
self._context = -1