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')
14 fp.write("auto eth0\n")
15 if (config.network.dhcp.value == _("yes")):
16 fp.write("iface eth0 inet dhcp\n")
18 fp.write("iface eth0 inet static\n")
19 fp.write(" address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
20 fp.write(" netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
21 fp.write(" gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
24 def loadNetworkConfig(self):
26 # parse the interfaces-file
27 fp = file('/etc/network/interfaces', 'r')
28 interfaces = fp.readlines()
34 split = i.strip().split(' ')
35 if (split[0] == "iface"):
38 if (len(split) == 4 and split[3] == "dhcp"):
39 ifaces[currif]["dhcp"] = "yes"
41 ifaces[currif]["dhcp"] = "no"
43 if (split[0] == "address"):
44 ifaces[currif]["address"] = map(int, split[1].split('.'))
45 if (split[0] == "netmask"):
46 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
47 if (split[0] == "gateway"):
48 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
50 # parse the resolv.conf-file
51 fp = file('/etc/network/interfaces', 'r')
52 resolv = fp.readlines()
59 split = i.strip().split(' ')
60 if (split[0] == "nameserver"):
61 config.network.nameserver.value = map(int, split[1].split('.'))
67 if (ifaces.has_key("eth0")):
68 if (ifaces["eth0"]["dhcp"] == "yes"):
69 config.network.dhcp.value = 1
71 config.network.dhcp.value = 0
72 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
73 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
74 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
78 def activateNetworkConfig(self):
80 os.system("/etc/init.d/networking restart")
81 config.network.ip.value = self.getCurrentIP()
83 def setDHCP(self, useDHCP):
86 config.network.ip.enabled = False
87 config.network.netmask.enabled = False
88 config.network.gateway.enabled = False
89 config.network.dns.enabled = False
91 print "NOT using DHCP"
92 config.network.ip.enabled = True
93 config.network.netmask.enabled = True
94 config.network.gateway.enabled = True
95 config.network.dns.enabled = True
97 def setIPNameserver(self, ip):
99 resolvconf = file('/etc/resolv.conf', 'w')
100 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
103 def setMACAddress(self, mac):
104 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
107 def setIPAddress(self, ip):
109 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
110 #self.writeNetworkConfig()
112 def setGateway(self, ip):
114 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
115 #self.writeNetworkConfig()
117 def setNetmask(self, ip):
119 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))
120 #self.writeNetworkConfig()
122 def getCurrentIP(self):
124 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
125 if x.split(':')[0] == "addr":
126 ipstr = x.split(':')[1].split('.')
130 print "[Network.py] got ip " + str(ip)
136 ip = iNetwork.getCurrentIP()
139 config.network = ConfigSubsection()
140 config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (_("no"), _("yes")))
141 config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
142 config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
143 config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
144 config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
145 config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
147 iNetwork.loadNetworkConfig()
149 #FIXME using this till other concept for this is implemented
150 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
151 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
154 def writeNetworkConfig(configElement):
155 iNetwork.writeNetworkConfig()
157 def setIPAddress(configElement):
158 iNetwork.setIPAddress(configElement.value)
160 def setGateway(configElement):
161 iNetwork.setGateway(configElement.value)
163 def setNetmask(configElement):
164 iNetwork.setNetmask(configElement.value)
166 def setDHCP(configElement):
167 iNetwork.setDHCP(configElement.value)
169 def setIPNameserver(configElement):
170 iNetwork.setIPNameserver(configElement.value)
172 def setMACAddress(configElement):
173 iNetwork.setMACAddress(configElement.value)
176 # this will call the "setup-val" initial
177 config.network.dhcp.addNotifier(setDHCP)
178 config.network.ip.addNotifier(setIPAddress)
179 config.network.netmask.addNotifier(setNetmask)
180 config.network.gateway.addNotifier(setGateway)
181 config.network.dns.addNotifier(setIPNameserver)
182 config.network.mac.addNotifier(setMACAddress)