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))
26 if (currentConfigSelectionElement(config.network.dhcp) == "yes"):
27 fp = file('/etc/resolv.conf', 'w')
28 fp.write("nameserver %d.%d.%d.%d\n" % tuple(config.network.dns.value))
32 def loadNetworkConfig(self):
34 # parse the interfaces-file
35 fp = file('/etc/network/interfaces', 'r')
36 interfaces = fp.readlines()
42 split = i.strip().split(' ')
43 if (split[0] == "iface"):
46 if (len(split) == 4 and split[3] == "dhcp"):
47 ifaces[currif]["dhcp"] = "yes"
49 ifaces[currif]["dhcp"] = "no"
51 if (split[0] == "address"):
52 ifaces[currif]["address"] = map(int, split[1].split('.'))
53 if (split[0] == "netmask"):
54 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
55 if (split[0] == "gateway"):
56 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
58 # parse the resolv.conf-file
59 fp = file('/etc/resolv.conf', 'r')
60 resolv = fp.readlines()
63 print "[Network.py] loading network files failed"
67 split = i.strip().split(' ')
68 if (split[0] == "nameserver"):
69 config.network.dns.value = map(int, split[1].split('.'))
71 print "[Network.py] resolv.conf parsing failed"
75 if (ifaces.has_key("eth0")):
76 if (ifaces["eth0"]["dhcp"] == "yes"):
77 config.network.dhcp.value = 1
79 config.network.dhcp.value = 0
80 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
81 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
82 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
84 print "[Network.py] parsing network failed"
86 def activateNetworkConfig(self):
88 os.system("/etc/init.d/networking restart")
89 config.network.ip.value = self.getCurrentIP()
91 def setDHCP(self, useDHCP):
94 config.network.ip.enabled = False
95 config.network.netmask.enabled = False
96 config.network.gateway.enabled = False
97 config.network.dns.enabled = False
99 print "NOT using DHCP"
100 config.network.ip.enabled = True
101 config.network.netmask.enabled = True
102 config.network.gateway.enabled = True
103 config.network.dns.enabled = True
105 def setIPNameserver(self, ip):
107 resolvconf = file('/etc/resolv.conf', 'w')
108 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
111 def setMACAddress(self, mac):
112 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
115 def setIPAddress(self, ip):
117 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
118 #self.writeNetworkConfig()
120 def setGateway(self, ip):
122 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
123 #self.writeNetworkConfig()
125 def setNetmask(self, ip):
127 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))
128 #self.writeNetworkConfig()
130 def getCurrentIP(self):
132 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
133 if x.split(':')[0] == "addr":
134 ipstr = x.split(':')[1].split('.')
138 print "[Network.py] got ip " + str(ip)
144 ip = iNetwork.getCurrentIP()
147 config.network = ConfigSubsection()
148 config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (("no", _("no")), ("yes", _("yes"))))
149 config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
150 config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
151 config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
152 config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
153 config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
155 iNetwork.loadNetworkConfig()
157 #FIXME using this till other concept for this is implemented
158 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
159 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
162 def writeNetworkConfig(configElement):
163 iNetwork.writeNetworkConfig()
165 def setIPAddress(configElement):
166 iNetwork.setIPAddress(configElement.value)
168 def setGateway(configElement):
169 iNetwork.setGateway(configElement.value)
171 def setNetmask(configElement):
172 iNetwork.setNetmask(configElement.value)
174 def setDHCP(configElement):
175 iNetwork.setDHCP(configElement.value)
177 def setIPNameserver(configElement):
178 iNetwork.setIPNameserver(configElement.value)
180 def setMACAddress(configElement):
181 iNetwork.setMACAddress(configElement.value)
184 # this will call the "setup-val" initial
185 config.network.dhcp.addNotifier(setDHCP)
186 config.network.ip.addNotifier(setIPAddress)
187 config.network.netmask.addNotifier(setNetmask)
188 config.network.gateway.addNotifier(setGateway)
189 config.network.dns.addNotifier(setIPNameserver)
190 config.network.mac.addNotifier(setMACAddress)