11 def getInterfaces(self):
12 devicesPattern = re.compile('[a-z]+[0-9]+')
14 fp = file('/proc/net/dev', 'r')
15 result = fp.readlines()
19 device = devicesPattern.search(line).group()
20 self.ifaces[device] = self.getDataForInterface(device)
21 except AttributeError:
24 print "self.ifaces:", self.ifaces
25 self.loadNetworkConfig()
26 #self.writeNetworkConfig()
32 def regExpMatch(self, pattern, string):
36 return pattern.search(string).group()
37 except AttributeError:
40 # helper function to convert ips from a sring to a list of ints
41 def convertIP(self, ip):
48 def getDataForInterface(self, iface):
49 #ipRegexp = '[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}'
50 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
51 ipLinePattern = re.compile('inet addr:' + ipRegexp)
52 netmaskLinePattern = re.compile('Mask:' + ipRegexp)
53 bcastLinePattern = re.compile('Bcast:' + ipRegexp)
54 ipPattern = re.compile(ipRegexp)
55 upPattern = re.compile('UP ')
56 macPattern = re.compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
58 fp = os.popen("ifconfig " + iface)
59 result = fp.readlines()
61 data = { 'up': False, 'dhcp': False }
63 ip = self.regExpMatch(ipPattern, self.regExpMatch(ipLinePattern, line))
64 netmask = self.regExpMatch(ipPattern, self.regExpMatch(netmaskLinePattern, line))
65 bcast = self.regExpMatch(ipPattern, self.regExpMatch(bcastLinePattern, line))
66 up = self.regExpMatch(upPattern, line)
67 mac = self.regExpMatch(macPattern, line)
69 data['ip'] = self.convertIP(ip)
70 if netmask is not None:
71 data['netmask'] = self.convertIP(netmask)
73 data['bcast'] = self.convertIP(bcast)
78 if not data.has_key('ip'):
80 data['ip'] = [192, 168, 1, 2]
81 data['netmask'] = [255, 255, 255, 0]
82 data['gateway'] = [192, 168, 1, 1]
84 fp = os.popen("route -n | grep " + iface)
85 result = fp.readlines()
89 if line[0:7] == "0.0.0.0":
90 gateway = self.regExpMatch(ipPattern, line[16:31])
91 if gateway is not None:
92 data['gateway'] = self.convertIP(gateway)
95 def writeNetworkConfig(self):
96 fp = file('/etc/network/interfaces', 'w')
97 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
99 fp.write("iface lo inet loopback\n\n")
101 for ifacename, iface in self.ifaces.items():
102 if iface['up'] == True:
103 fp.write("auto " + ifacename + "\n")
104 if iface['dhcp'] == True:
105 fp.write("iface "+ ifacename +" inet dhcp\n")
107 fp.write("iface "+ ifacename +" inet static\n")
108 if iface.has_key('ip'):
109 print tuple(iface['ip'])
110 fp.write(" address %d.%d.%d.%d\n" % tuple(iface['ip']))
111 fp.write(" netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
112 if iface.has_key('gateway'):
113 fp.write(" gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
115 if iface.has_key("configStrings"):
116 fp.write("\n" + iface["configStrings"] + "\n")
119 self.writeNameserverConfig()
122 def writeNameserverConfig(self):
123 fp = file('/etc/resolv.conf', 'w')
124 for nameserver in self.nameservers:
125 fp.write("nameserver %d.%d.%d.%d\n" % tuple(nameserver))
129 def loadNetworkConfig(self):
130 self.loadNameserverConfig()
133 # parse the interfaces-file
135 fp = file('/etc/network/interfaces', 'r')
136 interfaces = fp.readlines()
139 print "[Network.py] interfaces - opening failed"
144 split = i.strip().split(' ')
145 if (split[0] == "iface"):
148 if (len(split) == 4 and split[3] == "dhcp"):
149 ifaces[currif]["dhcp"] = True
151 ifaces[currif]["dhcp"] = False
153 if (split[0] == "address"):
154 ifaces[currif]["address"] = map(int, split[1].split('.'))
155 if (split[0] == "netmask"):
156 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
157 if (split[0] == "gateway"):
158 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
160 print "read interfaces:", ifaces
161 for ifacename, iface in ifaces.items():
162 if self.ifaces.has_key(ifacename):
163 self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
165 print "self.ifaces after loading:", self.ifaces
167 def loadNameserverConfig(self):
168 ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
169 nameserverPattern = re.compile("nameserver +" + ipRegexp)
170 ipPattern = re.compile(ipRegexp)
174 fp = file('/etc/resolv.conf', 'r')
175 resolv = fp.readlines()
177 self.nameservers = []
179 print "[Network.py] resolv.conf - opening failed"
182 if self.regExpMatch(nameserverPattern, line) is not None:
183 ip = self.regExpMatch(ipPattern, line)
185 self.nameservers.append(self.convertIP(ip))
187 print "nameservers:", self.nameservers
189 def deactivateNetworkConfig(self):
190 for iface in self.ifaces.keys():
191 os.system("ip addr flush " + iface)
192 os.system("/etc/init.d/networking stop")
193 os.system("killall -9 udhcpc")
194 os.system("rm /var/run/udhcpc*")
196 def activateNetworkConfig(self):
197 os.system("/etc/init.d/networking start")
199 def getNumberOfAdapters(self):
200 return len(self.ifaces)
202 def getAdapterName(self, iface):
205 def getAdapterList(self):
206 return self.ifaces.keys()
208 def getAdapterAttribute(self, iface, attribute):
209 if self.ifaces.has_key(iface):
210 if self.ifaces[iface].has_key(attribute):
211 return self.ifaces[iface][attribute]
214 def setAdapterAttribute(self, iface, attribute, value):
215 print "setting for adapter", iface, "attribute", attribute, " to value", value
216 if self.ifaces.has_key(iface):
217 self.ifaces[iface][attribute] = value
219 def removeAdapterAttribute(self, iface, attribute):
220 if self.ifaces.has_key(iface):
221 if self.ifaces[iface].has_key(attribute):
222 del self.ifaces[iface][attribute]
224 def getNameserverList(self):
225 return self.nameservers
227 def clearNameservers(self):
228 self.nameservers = []
230 def addNameserver(self, nameserver):
231 if nameserver not in self.nameservers:
232 self.nameservers.append(nameserver)
234 def removeNameserver(self, nameserver):
235 if nameserver in self.nameservers:
236 self.nameservers.remove(nameserver)
238 def changeNameserver(self, oldnameserver, newnameserver):
239 if oldnameserver in self.nameservers:
240 for i in range(len(self.nameservers)):
241 if self.nameservers[i] == oldnameserver:
242 self.nameservers[i] = newnameserver