1 from os import system, popen, path as os_path, listdir
2 from re import compile as re_compile
4 from enigma import eConsoleAppContainer
9 self.configuredInterfaces = {}
12 self.ethtool_bin = "/usr/sbin/ethtool"
13 self.container = eConsoleAppContainer()
15 def getInterfaces(self):
16 devicesPattern = re_compile('[a-z]+[0-9]+')
18 fp = file('/proc/net/dev', 'r')
19 result = fp.readlines()
23 device = devicesPattern.search(line).group()
26 self.ifaces[device] = self.getDataForInterface(device)
27 # Show only UP Interfaces in E2
28 #if self.getAdapterAttribute(device, 'up') is False:
29 # del self.ifaces[device]
30 except AttributeError:
33 print "self.ifaces:", self.ifaces
34 self.loadNetworkConfig()
35 #self.writeNetworkConfig()
41 def regExpMatch(self, pattern, string):
45 return pattern.search(string).group()
46 except AttributeError:
49 # helper function to convert ips from a sring to a list of ints
50 def convertIP(self, ip):
57 def getDataForInterface(self, iface):
58 #ipRegexp = '[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}'
59 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
60 ipLinePattern = re_compile('inet addr:' + ipRegexp)
61 netmaskLinePattern = re_compile('Mask:' + ipRegexp)
62 bcastLinePattern = re_compile('Bcast:' + ipRegexp)
63 ipPattern = re_compile(ipRegexp)
64 upPattern = re_compile('UP ')
65 macPattern = re_compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
67 fp = popen("ifconfig " + iface)
68 result = fp.readlines()
70 data = { 'up': False, 'dhcp': False }
72 ip = self.regExpMatch(ipPattern, self.regExpMatch(ipLinePattern, line))
73 netmask = self.regExpMatch(ipPattern, self.regExpMatch(netmaskLinePattern, line))
74 bcast = self.regExpMatch(ipPattern, self.regExpMatch(bcastLinePattern, line))
75 up = self.regExpMatch(upPattern, line)
76 mac = self.regExpMatch(macPattern, line)
78 data['ip'] = self.convertIP(ip)
79 if netmask is not None:
80 data['netmask'] = self.convertIP(netmask)
82 data['bcast'] = self.convertIP(bcast)
87 if not data.has_key('ip'):
89 data['ip'] = [192, 168, 1, 2]
90 data['netmask'] = [255, 255, 255, 0]
91 data['gateway'] = [192, 168, 1, 1]
93 fp = popen("route -n | grep " + iface)
94 result = fp.readlines()
98 if line[0:7] == "0.0.0.0":
99 gateway = self.regExpMatch(ipPattern, line[16:31])
100 if gateway is not None:
101 data['gateway'] = self.convertIP(gateway)
104 def writeNetworkConfig(self):
105 fp = file('/etc/network/interfaces', 'w')
106 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
107 fp.write("auto lo\n")
108 fp.write("iface lo inet loopback\n\n")
109 for ifacename, iface in self.ifaces.items():
110 if iface['up'] == True:
111 fp.write("auto " + ifacename + "\n")
112 if iface['dhcp'] == True:
113 fp.write("iface "+ ifacename +" inet dhcp\n")
114 if iface['dhcp'] == False:
115 fp.write("iface "+ ifacename +" inet static\n")
116 if iface.has_key('ip'):
117 print tuple(iface['ip'])
118 fp.write(" address %d.%d.%d.%d\n" % tuple(iface['ip']))
119 fp.write(" netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
120 if iface.has_key('gateway'):
121 fp.write(" gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
122 if iface.has_key("configStrings"):
123 fp.write("\n" + iface["configStrings"] + "\n")
126 self.writeNameserverConfig()
128 def writeNameserverConfig(self):
129 fp = file('/etc/resolv.conf', 'w')
130 for nameserver in self.nameservers:
131 fp.write("nameserver %d.%d.%d.%d\n" % tuple(nameserver))
134 def loadNetworkConfig(self):
135 self.loadNameserverConfig()
137 # parse the interfaces-file
139 fp = file('/etc/network/interfaces', 'r')
140 interfaces = fp.readlines()
143 print "[Network.py] interfaces - opening failed"
148 split = i.strip().split(' ')
149 if (split[0] == "iface"):
152 if (len(split) == 4 and split[3] == "dhcp"):
153 ifaces[currif]["dhcp"] = True
155 ifaces[currif]["dhcp"] = False
157 if (split[0] == "address"):
158 ifaces[currif]["address"] = map(int, split[1].split('.'))
159 if (split[0] == "netmask"):
160 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
161 if (split[0] == "gateway"):
162 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
164 self.configuredInterfaces = ifaces
165 print "read interfaces:", ifaces
166 for ifacename, iface in ifaces.items():
167 if self.ifaces.has_key(ifacename):
168 self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
170 print "self.ifaces after loading:", self.ifaces
172 def loadNameserverConfig(self):
173 ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
174 nameserverPattern = re_compile("nameserver +" + ipRegexp)
175 ipPattern = re_compile(ipRegexp)
179 fp = file('/etc/resolv.conf', 'r')
180 resolv = fp.readlines()
182 self.nameservers = []
184 print "[Network.py] resolv.conf - opening failed"
187 if self.regExpMatch(nameserverPattern, line) is not None:
188 ip = self.regExpMatch(ipPattern, line)
190 self.nameservers.append(self.convertIP(ip))
192 print "nameservers:", self.nameservers
194 def deactivateNetworkConfig(self):
195 for iface in self.ifaces.keys():
196 system("ip addr flush " + iface)
197 system("/etc/init.d/networking stop")
198 system("killall -9 udhcpc")
199 system("rm /var/run/udhcpc*")
201 def activateNetworkConfig(self):
202 system("/etc/init.d/networking start")
205 def getNumberOfAdapters(self):
206 return len(self.ifaces)
208 def getFriendlyAdapterName(self, x):
209 # maybe this needs to be replaced by an external list.
211 "eth0": _("Integrated Ethernet"),
212 "wlan0": _("Wireless"),
213 "ath0": _("Integrated Wireless")
215 return friendlyNames.get(x, x) # when we have no friendly name, use adapter name
217 def getAdapterName(self, iface):
220 def getAdapterList(self):
221 return self.ifaces.keys()
223 def getAdapterAttribute(self, iface, attribute):
224 if self.ifaces.has_key(iface):
225 if self.ifaces[iface].has_key(attribute):
226 return self.ifaces[iface][attribute]
229 def setAdapterAttribute(self, iface, attribute, value):
230 print "setting for adapter", iface, "attribute", attribute, " to value", value
231 if self.ifaces.has_key(iface):
232 self.ifaces[iface][attribute] = value
234 def removeAdapterAttribute(self, iface, attribute):
235 if self.ifaces.has_key(iface):
236 if self.ifaces[iface].has_key(attribute):
237 del self.ifaces[iface][attribute]
239 def getNameserverList(self):
240 if len(self.nameservers) == 0:
241 return [[0, 0, 0, 0], [0, 0, 0, 0]]
243 return self.nameservers
245 def clearNameservers(self):
246 self.nameservers = []
248 def addNameserver(self, nameserver):
249 if nameserver not in self.nameservers:
250 self.nameservers.append(nameserver)
252 def removeNameserver(self, nameserver):
253 if nameserver in self.nameservers:
254 self.nameservers.remove(nameserver)
256 def changeNameserver(self, oldnameserver, newnameserver):
257 if oldnameserver in self.nameservers:
258 for i in range(len(self.nameservers)):
259 if self.nameservers[i] == oldnameserver:
260 self.nameservers[i] = newnameserver
262 def writeDefaultNetworkConfig(self,mode='lan'):
263 fp = file('/etc/network/interfaces', 'w')
264 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
265 fp.write("auto lo\n")
266 fp.write("iface lo inet loopback\n\n")
268 fp.write("auto wlan0\n")
269 fp.write("iface wlan0 inet dhcp\n")
270 if mode == 'wlan-mpci':
271 fp.write("auto ath0\n")
272 fp.write("iface ath0 inet dhcp\n")
274 fp.write("auto eth0\n")
275 fp.write("iface eth0 inet dhcp\n")
279 def resetNetworkConfig(self,mode='lan'):
280 self.deactivateNetworkConfig()
281 self.writeDefaultNetworkConfig(mode)
283 system("ifconfig eth0 down")
284 system("ifconfig ath0 down")
285 system("ifconfig wlan0 up")
286 if mode == 'wlan-mpci':
287 system("ifconfig eth0 down")
288 system("ifconfig wlan0 down")
289 system("ifconfig ath0 up")
291 system("ifconfig eth0 up")
292 system("ifconfig wlan0 down")
293 system("ifconfig ath0 down")
296 def checkNetworkState(self):
297 # www.dream-multimedia-tv.de, www.heise.de, www.google.de
298 return system("ping -c 1 82.149.226.170") == 0 or \
299 system("ping -c 1 193.99.144.85") == 0 or \
300 system("ping -c 1 209.85.135.103") == 0
302 def restartNetwork(self):
303 iNetwork.deactivateNetworkConfig()
304 iNetwork.activateNetworkConfig()
306 def getLinkState(self,iface,callback):
307 self.dataAvail = callback
308 cmd = self.ethtool_bin + " " + iface
309 self.container.appClosed.get().append(self.cmdFinished)
310 self.container.dataAvail.get().append(callback)
311 self.container.execute(cmd)
313 def cmdFinished(self,retval):
314 self.container.appClosed.get().remove(self.cmdFinished)
315 self.container.dataAvail.get().remove(self.dataAvail)
317 def checkforInterface(self,iface):
318 if self.getAdapterAttribute(iface, 'up') is True:
321 ret=system("ifconfig " + iface + " up")
322 system("ifconfig " + iface + " down")
328 def checkDNSLookup(self):
329 return system("nslookup www.dream-multimedia-tv.de") == 0 or \
330 system("nslookup www.heise.de") == 0 or \
331 system("nslookup www.google.de")
333 def deactivateInterface(self,iface):
334 system("ifconfig " + iface + " down")
336 def detectWlanModule(self):
337 self.wlanmodule = None
338 rt73_dir = "/sys/bus/usb/drivers/rt73/"
339 zd1211b_dir = "/sys/bus/usb/drivers/zd1211b/"
340 madwifi_dir = "/sys/bus/pci/drivers/ath_pci/"
341 if os_path.exists(madwifi_dir):
342 files = listdir(madwifi_dir)
344 self.wlanmodule = 'madwifi'
345 elif os_path.exists(rt73_dir):
346 files = listdir(rt73_dir)
348 self.wlanmodule = 'ralink'
349 elif os_path.exists(zd1211b_dir):
350 files = listdir(zd1211b_dir)
352 self.wlanmodule = 'zydas'
353 return self.wlanmodule