import os
import re
from socket import *
+from enigma import eTimer, eConsoleAppContainer
class Network:
def __init__(self):
self.ifaces = {}
self.nameservers = []
self.getInterfaces()
-
+ self.ethtool_bin = "/usr/sbin/ethtool"
+ self.container = eConsoleAppContainer()
+
def getInterfaces(self):
devicesPattern = re.compile('[a-z]+[0-9]+')
-
+
fp = file('/proc/net/dev', 'r')
result = fp.readlines()
fp.close()
try:
device = devicesPattern.search(line).group()
self.ifaces[device] = self.getDataForInterface(device)
+ # Show only UP Interfaces in E2
+ if self.getAdapterAttribute(device, 'up') is False:
+ del self.ifaces[device]
except AttributeError:
pass
-
+
print "self.ifaces:", self.ifaces
self.loadNetworkConfig()
#self.writeNetworkConfig()
#print ord(' ')
#for line in result:
-# print ord(line[0])
+ # print ord(line[0])
# helper function
def regExpMatch(self, pattern, string):
return pattern.search(string).group()
except AttributeError:
None
-
+
# helper function to convert ips from a sring to a list of ints
def convertIP(self, ip):
strIP = ip.split('.')
ipPattern = re.compile(ipRegexp)
upPattern = re.compile('UP ')
macPattern = re.compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
-
+
fp = os.popen("ifconfig " + iface)
result = fp.readlines()
fp.close()
data['ip'] = [192, 168, 1, 2]
data['netmask'] = [255, 255, 255, 0]
data['gateway'] = [192, 168, 1, 1]
-
+
fp = os.popen("route -n | grep " + iface)
result = fp.readlines()
- fp.close()
+ fp.close()
for line in result:
print line[0:7]
if line[0:7] == "0.0.0.0":
fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
fp.write("auto lo\n")
fp.write("iface lo inet loopback\n\n")
-
+
for ifacename, iface in self.ifaces.items():
if iface['up'] == True:
fp.write("auto " + ifacename + "\n")
fp.write(" netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
if iface.has_key('gateway'):
fp.write(" gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
-
+
if iface.has_key("configStrings"):
fp.write("\n" + iface["configStrings"] + "\n")
fp.write("\n")
fp.close()
self.writeNameserverConfig()
-
def writeNameserverConfig(self):
fp = file('/etc/resolv.conf', 'w')
for nameserver in self.nameservers:
fp.write("nameserver %d.%d.%d.%d\n" % tuple(nameserver))
fp.close()
-
def loadNetworkConfig(self):
self.loadNameserverConfig()
-
interfaces = []
# parse the interfaces-file
try:
fp.close()
except:
print "[Network.py] interfaces - opening failed"
-
+
ifaces = {}
currif = ""
for i in interfaces:
if (split[0] == "netmask"):
ifaces[currif]["netmask"] = map(int, split[1].split('.'))
if (split[0] == "gateway"):
- ifaces[currif]["gateway"] = map(int, split[1].split('.'))
-
+ ifaces[currif]["gateway"] = map(int, split[1].split('.'))
+
print "read interfaces:", ifaces
for ifacename, iface in ifaces.items():
if self.ifaces.has_key(ifacename):
self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
-
+
print "self.ifaces after loading:", self.ifaces
def loadNameserverConfig(self):
ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
nameserverPattern = re.compile("nameserver +" + ipRegexp)
ipPattern = re.compile(ipRegexp)
-
+
resolv = []
try:
fp = file('/etc/resolv.conf', 'r')
self.nameservers = []
except:
print "[Network.py] resolv.conf - opening failed"
-
+
for line in resolv:
if self.regExpMatch(nameserverPattern, line) is not None:
ip = self.regExpMatch(ipPattern, line)
if ip is not None:
self.nameservers.append(self.convertIP(ip))
-
- print "nameservers:", self.nameservers
+
+ print "nameservers:", self.nameservers
def deactivateNetworkConfig(self):
for iface in self.ifaces.keys():
def activateNetworkConfig(self):
os.system("/etc/init.d/networking start")
-
+ self.getInterfaces()
+
def getNumberOfAdapters(self):
return len(self.ifaces)
def getAdapterName(self, iface):
return iface
-
+
def getAdapterList(self):
return self.ifaces.keys()
-
+
def getAdapterAttribute(self, iface, attribute):
if self.ifaces.has_key(iface):
if self.ifaces[iface].has_key(attribute):
return self.ifaces[iface][attribute]
return None
-
+
def setAdapterAttribute(self, iface, attribute, value):
print "setting for adapter", iface, "attribute", attribute, " to value", value
if self.ifaces.has_key(iface):
if self.ifaces.has_key(iface):
if self.ifaces[iface].has_key(attribute):
del self.ifaces[iface][attribute]
-
+
def getNameserverList(self):
- return self.nameservers
-
+ if len(self.nameservers) == 0:
+ return [[0, 0, 0, 0], [0, 0, 0, 0]]
+ else:
+ return self.nameservers
+
def clearNameservers(self):
self.nameservers = []
-
+
def addNameserver(self, nameserver):
if nameserver not in self.nameservers:
self.nameservers.append(nameserver)
-
+
def removeNameserver(self, nameserver):
if nameserver in self.nameservers:
self.nameservers.remove(nameserver)
-
+
def changeNameserver(self, oldnameserver, newnameserver):
if oldnameserver in self.nameservers:
for i in range(len(self.nameservers)):
if self.nameservers[i] == oldnameserver:
self.nameservers[i] = newnameserver
+ def writeDefaultNetworkConfig(self,mode='lan'):
+ fp = file('/etc/network/interfaces', 'w')
+ fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
+ fp.write("auto lo\n")
+ fp.write("iface lo inet loopback\n\n")
+ if mode == 'wlan':
+ fp.write("auto wlan0\n")
+ fp.write("iface wlan0 inet dhcp\n")
+ else:
+ fp.write("auto eth0\n")
+ fp.write("iface eth0 inet dhcp\n")
+ fp.write("\n")
+ fp.close()
+
+ def resetNetworkConfig(self,mode='lan'):
+ self.deactivateNetworkConfig()
+ self.writeDefaultNetworkConfig(mode)
+ if mode == 'wlan':
+ os.system("ifconfig eth0 down")
+ os.system("ifconfig wlan0 up")
+ else:
+ os.system("ifconfig eth0 up")
+ os.system("ifconfig wlan0 down")
+
+ def checkNetworkState(self):
+ ret=os.system("ping -c 1 www.dream-multimedia-tv.de")
+ if ret == 0:
+ return True
+ else:
+ return False
+
+ def restartNetwork(self):
+ iNetwork.deactivateNetworkConfig()
+ iNetwork.activateNetworkConfig()
+
+ def getLinkState(self,iface,callback):
+ self.dataAvail = callback
+ cmd = self.ethtool_bin + " " + iface
+ self.container.appClosed.get().append(self.cmdFinished)
+ self.container.dataAvail.get().append(callback)
+ self.container.execute(cmd)
+
+ def cmdFinished(self,retval):
+ self.container.appClosed.get().remove(self.cmdFinished)
+ self.container.dataAvail.get().remove(self.dataAvail)
+
iNetwork = Network()
def InitNetwork():