1 from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigMAC
13 def getInterfaces(self):
14 devicesPattern = re.compile('[a-z]+[0-9]+')
16 fp = file('/proc/net/dev', 'r')
17 result = fp.readlines()
21 device = devicesPattern.search(line).group()
22 self.ifaces[device] = self.getDataForInterface(device)
23 except AttributeError:
26 print "self.ifaces:", self.ifaces
27 self.loadNetworkConfig()
28 #self.writeNetworkConfig()
34 def regExpMatch(self, pattern, string):
38 return pattern.search(string).group()
39 except AttributeError:
42 # helper function to convert ips from a sring to a list of ints
43 def convertIP(self, ip):
50 def getDataForInterface(self, iface):
51 #ipRegexp = '[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}'
52 ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
53 ipLinePattern = re.compile('inet addr:' + ipRegexp)
54 netmaskLinePattern = re.compile('Mask:' + ipRegexp)
55 bcastLinePattern = re.compile('Bcast:' + ipRegexp)
56 ipPattern = re.compile(ipRegexp)
57 upPattern = re.compile('UP ')
58 macPattern = re.compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
60 fp = os.popen("ifconfig " + iface)
61 result = fp.readlines()
63 data = { 'up': False, 'dhcp': False }
65 ip = self.regExpMatch(ipPattern, self.regExpMatch(ipLinePattern, line))
66 netmask = self.regExpMatch(ipPattern, self.regExpMatch(netmaskLinePattern, line))
67 bcast = self.regExpMatch(ipPattern, self.regExpMatch(bcastLinePattern, line))
68 up = self.regExpMatch(upPattern, line)
69 mac = self.regExpMatch(macPattern, line)
71 data['ip'] = self.convertIP(ip)
72 if netmask is not None:
73 data['netmask'] = self.convertIP(netmask)
75 data['bcast'] = self.convertIP(bcast)
80 if not data.has_key('ip'):
82 data['ip'] = [192, 168, 1, 2]
83 data['netmask'] = [255, 255, 255, 0]
84 data['gateway'] = [192, 168, 1, 1]
86 fp = os.popen("route -n | grep " + iface)
87 result = fp.readlines()
91 if line[0:7] == "0.0.0.0":
92 gateway = self.regExpMatch(ipPattern, line[16:31])
93 if gateway is not None:
94 data['gateway'] = self.convertIP(gateway)
97 def writeNetworkConfig(self):
98 fp = file('/etc/network/interfaces', 'w')
99 fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
100 fp.write("auto lo\n")
101 fp.write("iface lo inet loopback\n\n")
103 for ifacename, iface in self.ifaces.items():
104 if iface['up'] == True:
105 fp.write("auto " + ifacename + "\n")
106 if iface['dhcp'] == True:
107 fp.write("iface "+ ifacename +" inet dhcp\n")
109 fp.write("iface "+ ifacename +" inet static\n")
110 if iface.has_key('ip'):
111 print tuple(iface['ip'])
112 fp.write(" address %d.%d.%d.%d\n" % tuple(iface['ip']))
113 fp.write(" netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
114 if iface.has_key('gateway'):
115 fp.write(" gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
117 if iface.has_key("configStrings"):
118 fp.write("\n" + iface["configStrings"] + "\n")
121 self.writeNameserverConfig()
124 def writeNameserverConfig(self):
125 fp = file('/etc/resolv.conf', 'w')
126 for nameserver in self.nameservers:
127 fp.write("nameserver %d.%d.%d.%d\n" % tuple(nameserver))
131 def loadNetworkConfig(self):
132 self.loadNameserverConfig()
135 # parse the interfaces-file
137 fp = file('/etc/network/interfaces', 'r')
138 interfaces = fp.readlines()
141 print "[Network.py] interfaces - opening failed"
146 split = i.strip().split(' ')
147 if (split[0] == "iface"):
150 if (len(split) == 4 and split[3] == "dhcp"):
151 ifaces[currif]["dhcp"] = True
153 ifaces[currif]["dhcp"] = False
155 if (split[0] == "address"):
156 ifaces[currif]["address"] = map(int, split[1].split('.'))
157 if (split[0] == "netmask"):
158 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
159 if (split[0] == "gateway"):
160 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
162 print "read interfaces:", ifaces
163 for ifacename, iface in ifaces.items():
164 if self.ifaces.has_key(ifacename):
165 self.ifaces[ifacename]["dhcp"] = iface["dhcp"]
167 print "self.ifaces after loading:", self.ifaces
169 def loadNameserverConfig(self):
170 ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
171 nameserverPattern = re.compile("nameserver +" + ipRegexp)
172 ipPattern = re.compile(ipRegexp)
176 fp = file('/etc/resolv.conf', 'r')
177 resolv = fp.readlines()
179 self.nameservers = []
181 print "[Network.py] resolv.conf - opening failed"
184 if self.regExpMatch(nameserverPattern, line) is not None:
185 ip = self.regExpMatch(ipPattern, line)
187 self.nameservers.append(self.convertIP(ip))
189 print "nameservers:", self.nameservers
191 def deactivateNetworkConfig(self):
192 for iface in self.ifaces.keys():
193 os.system("ip addr flush " + iface)
194 os.system("/etc/init.d/networking stop")
195 os.system("killall -9 udhcpc")
196 os.system("rm /var/run/udhcpc*")
198 def activateNetworkConfig(self):
199 os.system("/etc/init.d/networking start")
201 def getNumberOfAdapters(self):
202 return len(self.ifaces)
204 def getAdapterName(self, iface):
207 def getAdapterList(self):
208 return self.ifaces.keys()
210 def getAdapterAttribute(self, iface, attribute):
211 if self.ifaces.has_key(iface):
212 if self.ifaces[iface].has_key(attribute):
213 return self.ifaces[iface][attribute]
216 def setAdapterAttribute(self, iface, attribute, value):
217 print "setting for adapter", iface, "attribute", attribute, " to value", value
218 if self.ifaces.has_key(iface):
219 self.ifaces[iface][attribute] = value
221 def removeAdapterAttribute(self, iface, attribute):
222 if self.ifaces.has_key(iface):
223 if self.ifaces[iface].has_key(attribute):
224 del self.ifaces[iface][attribute]
226 def getNameserverList(self):
227 return self.nameservers
229 def clearNameservers(self):
230 self.nameservers = []
232 def addNameserver(self, nameserver):
233 if nameserver not in self.nameservers:
234 self.nameservers.append(nameserver)
236 def removeNameserver(self, nameserver):
237 if nameserver in self.nameservers:
238 self.nameservers.remove(nameserver)
240 def changeNameserver(self, oldnameserver, newnameserver):
241 if oldnameserver in self.nameservers:
242 for i in range(len(self.nameservers)):
243 if self.nameservers[i] == oldnameserver:
244 self.nameservers[i] = newnameserver