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):
125 print gethostbyname(gethostname())
126 ip = gethostbyname(gethostname()).split('.')
128 print "[Network.py] Could not get current ip (not necessarily an error)"
129 print "[Network.py] got ip " + str(ip)
135 ip = iNetwork.getCurrentIP()
138 config.network = ConfigSubsection()
139 config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (_("no"), _("yes")))
140 config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
141 config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
142 config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
143 config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
144 config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
146 iNetwork.loadNetworkConfig()
148 #FIXME using this till other concept for this is implemented
149 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
150 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
153 def writeNetworkConfig(configElement):
154 iNetwork.writeNetworkConfig()
156 def setIPAddress(configElement):
157 iNetwork.setIPAddress(configElement.value)
159 def setGateway(configElement):
160 iNetwork.setGateway(configElement.value)
162 def setNetmask(configElement):
163 iNetwork.setNetmask(configElement.value)
165 def setDHCP(configElement):
166 iNetwork.setDHCP(configElement.value)
168 def setIPNameserver(configElement):
169 iNetwork.setIPNameserver(configElement.value)
171 def setMACAddress(configElement):
172 iNetwork.setMACAddress(configElement.value)
175 # this will call the "setup-val" initial
176 config.network.dhcp.addNotifier(setDHCP)
177 config.network.ip.addNotifier(setIPAddress)
178 config.network.netmask.addNotifier(setNetmask)
179 config.network.gateway.addNotifier(setGateway)
180 config.network.dns.addNotifier(setIPNameserver)
181 config.network.mac.addNotifier(setMACAddress)