10 def writeNetworkConfig(self):
11 # fixme restarting and updating the network too often. possible fix: check current config and execute only if changed :/
12 # fixme using interfaces.tmp instead of interfaces for now
13 fp = file('/etc/network/interfaces', 'w')
15 fp.write("iface lo inet loopback\n\n")
16 fp.write("auto eth0\n")
17 if (currentConfigSelectionElement(config.network.dhcp) == "yes"):
18 fp.write("iface eth0 inet dhcp\n")
20 fp.write("iface eth0 inet static\n")
21 fp.write(" address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
22 fp.write(" netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
23 fp.write(" gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
24 fp2 = file('/etc/resolv.conf', 'w')
25 fp2.write("nameserver %d.%d.%d.%d\n" % tuple(config.network.dns.value))
29 def loadNetworkConfig(self):
31 # parse the interfaces-file
32 fp = file('/etc/network/interfaces', 'r')
33 interfaces = fp.readlines()
39 split = i.strip().split(' ')
40 if (split[0] == "iface"):
43 if (len(split) == 4 and split[3] == "dhcp"):
44 ifaces[currif]["dhcp"] = "yes"
46 ifaces[currif]["dhcp"] = "no"
48 if (split[0] == "address"):
49 ifaces[currif]["address"] = map(int, split[1].split('.'))
50 if (split[0] == "netmask"):
51 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
52 if (split[0] == "gateway"):
53 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
55 # parse the resolv.conf-file
56 fp = file('/etc/resolv.conf', 'r')
57 resolv = fp.readlines()
60 print "[Network.py] loading network files failed"
64 split = i.strip().split(' ')
65 if (split[0] == "nameserver"):
66 config.network.dns.value = map(int, split[1].split('.'))
68 print "[Network.py] resolv.conf parsing failed"
72 if (ifaces.has_key("eth0")):
73 if (ifaces["eth0"]["dhcp"] == "yes"):
74 config.network.dhcp.value = 1
76 config.network.dhcp.value = 0
77 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
78 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
79 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
81 print "[Network.py] parsing network failed"
83 def activateNetworkConfig(self):
85 os.system("/etc/init.d/networking restart")
86 config.network.ip.value = self.getCurrentIP()
88 def setDHCP(self, useDHCP):
91 config.network.ip.enabled = False
92 config.network.netmask.enabled = False
93 config.network.gateway.enabled = False
94 config.network.dns.enabled = False
96 print "NOT using DHCP"
97 config.network.ip.enabled = True
98 config.network.netmask.enabled = True
99 config.network.gateway.enabled = True
100 config.network.dns.enabled = True
102 def setIPNameserver(self, ip):
104 resolvconf = file('/etc/resolv.conf', 'w')
105 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
108 def setMACAddress(self, mac):
109 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
112 def setIPAddress(self, ip):
114 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
115 #self.writeNetworkConfig()
117 def setGateway(self, ip):
119 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
120 #self.writeNetworkConfig()
122 def setNetmask(self, ip):
124 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))
125 #self.writeNetworkConfig()
127 def getCurrentIP(self):
129 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
130 if x.split(':')[0] == "addr":
131 ipstr = x.split(':')[1].split('.')
135 print "[Network.py] got ip " + str(ip)
141 ip = iNetwork.getCurrentIP()
144 config.network = ConfigSubsection()
145 config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (("no", _("no")), ("yes", _("yes"))))
146 config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
147 config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
148 config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
149 config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
150 config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
152 iNetwork.loadNetworkConfig()
154 #FIXME using this till other concept for this is implemented
155 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
156 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
159 def writeNetworkConfig(configElement):
160 iNetwork.writeNetworkConfig()
162 def setIPAddress(configElement):
163 iNetwork.setIPAddress(configElement.value)
165 def setGateway(configElement):
166 iNetwork.setGateway(configElement.value)
168 def setNetmask(configElement):
169 iNetwork.setNetmask(configElement.value)
171 def setDHCP(configElement):
172 iNetwork.setDHCP(configElement.value)
174 def setIPNameserver(configElement):
175 iNetwork.setIPNameserver(configElement.value)
177 def setMACAddress(configElement):
178 iNetwork.setMACAddress(configElement.value)
181 # this will call the "setup-val" initial
182 config.network.dhcp.addNotifier(setDHCP)
183 config.network.ip.addNotifier(setIPAddress)
184 config.network.netmask.addNotifier(setNetmask)
185 config.network.gateway.addNotifier(setGateway)
186 config.network.dns.addNotifier(setIPNameserver)
187 config.network.mac.addNotifier(setMACAddress)