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")
17 fp.write(" address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
19 fp.write("iface eth0 inet static\n")
20 fp.write(" address %d.%d.%d.%d\n" % tuple(config.network.ip.value))
21 fp.write(" netmask %d.%d.%d.%d\n" % tuple(config.network.netmask.value))
22 fp.write(" gateway %d.%d.%d.%d\n" % tuple(config.network.gateway.value))
25 def loadNetworkConfig(self):
27 # parse the interfaces-file
28 fp = file('/etc/network/interfaces', 'r')
29 interfaces = fp.readlines()
35 split = i.strip().split(' ')
36 if (split[0] == "iface"):
39 if (len(split) == 4 and split[3] == "dhcp"):
40 ifaces[currif]["dhcp"] = "yes"
42 ifaces[currif]["dhcp"] = "no"
44 if (split[0] == "address"):
45 ifaces[currif]["address"] = map(int, split[1].split('.'))
46 if (split[0] == "netmask"):
47 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
48 if (split[0] == "gateway"):
49 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
51 # parse the resolv.conf-file
52 fp = file('/etc/network/interfaces', 'r')
53 resolv = fp.readlines()
60 split = i.strip().split(' ')
61 if (split[0] == "nameserver"):
62 config.network.nameserver.value = map(int, split[1].split('.'))
68 if (ifaces.has_key("eth0")):
69 if (ifaces["eth0"]["dhcp"] == "yes"):
70 config.network.dhcp.value = 1
72 config.network.dhcp.value = 0
73 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
74 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
75 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
79 def activateNetworkConfig(self):
81 os.system("/etc/init.d/networking restart")
82 config.network.ip.value = self.getCurrentIP()
84 def setDHCP(self, useDHCP):
87 config.network.ip.enabled = False
88 config.network.netmask.enabled = False
89 config.network.gateway.enabled = False
90 config.network.dns.enabled = False
92 print "NOT using DHCP"
93 config.network.ip.enabled = True
94 config.network.netmask.enabled = True
95 config.network.gateway.enabled = True
96 config.network.dns.enabled = True
98 def setIPNameserver(self, ip):
100 resolvconf = file('/etc/resolv.conf', 'w')
101 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
104 def setMACAddress(self, mac):
105 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
108 def setIPAddress(self, ip):
110 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
111 #self.writeNetworkConfig()
113 def setGateway(self, ip):
115 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
116 #self.writeNetworkConfig()
118 def setNetmask(self, ip):
120 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))
121 #self.writeNetworkConfig()
123 def getCurrentIP(self):
126 print gethostbyname(gethostname())
127 ip = gethostbyname(gethostname()).split('.')
129 print "[Network.py] Could not get current ip (not necessarily an error)"
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)