-def InitNetwork():
- config.network = ConfigSubsection()
- config.network.ip = configElement("config.network.ip", configSequence, [192,168,1,45], (("."), 3))
- config.network.gateway = configElement("config.network.gateway", configSequence, [192,168,1,3], (("."), 3))
- config.network.dns = configElement("config.network.dns", configSequence, [192,168,1,3], (("."), 3))
- config.network.mac = configElement("config.network.mac", configSequence, [00,11,22,33,44,55], ((":"), 2))
+ fp = os.popen("route -n | grep " + iface)
+ result = fp.readlines()
+ fp.close()
+ for line in result:
+ print line[0:7]
+ if line[0:7] == "0.0.0.0":
+ gateway = self.regExpMatch(ipPattern, line[16:31])
+ if gateway is not None:
+ data['gateway'] = self.convertIP(gateway)
+ return data
+
+ def writeNetworkConfig(self):
+ 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")
+
+ for ifacename, iface in self.ifaces.items():
+ if iface['up'] == True:
+ fp.write("auto " + ifacename + "\n")
+ if iface['dhcp'] == True:
+ fp.write("iface "+ ifacename +" inet dhcp\n")
+ else:
+ fp.write("iface "+ ifacename +" inet static\n")
+ if iface.has_key('ip'):
+ print tuple(iface['ip'])
+ fp.write(" address %d.%d.%d.%d\n" % tuple(iface['ip']))
+ 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 = file('/etc/network/interfaces', 'r')
+ interfaces = fp.readlines()
+ fp.close()
+ except:
+ print "[Network.py] interfaces - opening failed"
+
+ ifaces = {}
+ currif = ""
+ for i in interfaces:
+ split = i.strip().split(' ')
+ if (split[0] == "iface"):
+ currif = split[1]
+ ifaces[currif] = {}
+ if (len(split) == 4 and split[3] == "dhcp"):
+ ifaces[currif]["dhcp"] = True
+ else:
+ ifaces[currif]["dhcp"] = False
+ if (currif != ""):
+ if (split[0] == "address"):
+ ifaces[currif]["address"] = map(int, split[1].split('.'))
+ if (split[0] == "netmask"):
+ ifaces[currif]["netmask"] = map(int, split[1].split('.'))
+ if (split[0] == "gateway"):
+ 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')
+ resolv = fp.readlines()
+ fp.close()
+ 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
+
+ def deactivateNetworkConfig(self):
+ for iface in self.ifaces.keys():
+ os.system("ip addr flush " + iface)
+ os.system("/etc/init.d/networking stop")
+ os.system("killall -9 udhcpc")
+ os.system("rm /var/run/udhcpc*")