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 == 1):
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 if config.network.dhcp.value == 0:
25 fp = file('/etc/resolv.conf', 'w')
26 fp.write("nameserver %d.%d.%d.%d\n" % tuple(config.network.dns.value))
30 def loadNetworkConfig(self):
32 # parse the interfaces-file
33 fp = file('/etc/network/interfaces', 'r')
34 interfaces = fp.readlines()
40 split = i.strip().split(' ')
41 if (split[0] == "iface"):
44 if (len(split) == 4 and split[3] == "dhcp"):
45 ifaces[currif]["dhcp"] = "yes"
47 ifaces[currif]["dhcp"] = "no"
49 if (split[0] == "address"):
50 ifaces[currif]["address"] = map(int, split[1].split('.'))
51 if (split[0] == "netmask"):
52 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
53 if (split[0] == "gateway"):
54 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
56 # parse the resolv.conf-file
57 fp = file('/etc/resolv.conf', 'r')
58 resolv = fp.readlines()
65 split = i.strip().split(' ')
66 if (split[0] == "nameserver"):
67 config.network.nameserver.value = map(int, split[1].split('.'))
73 if (ifaces.has_key("eth0")):
74 if (ifaces["eth0"]["dhcp"] == "yes"):
75 config.network.dhcp.value = 1
77 config.network.dhcp.value = 0
78 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
79 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
80 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
84 def activateNetworkConfig(self):
86 os.system("/etc/init.d/networking restart")
87 config.network.ip.value = self.getCurrentIP()
89 def setDHCP(self, useDHCP):
92 config.network.ip.enabled = False
93 config.network.netmask.enabled = False
94 config.network.gateway.enabled = False
95 config.network.dns.enabled = False
97 print "NOT using DHCP"
98 config.network.ip.enabled = True
99 config.network.netmask.enabled = True
100 config.network.gateway.enabled = True
101 config.network.dns.enabled = True
103 def setIPNameserver(self, ip):
105 resolvconf = file('/etc/resolv.conf', 'w')
106 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
109 def setMACAddress(self, mac):
110 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
113 def setIPAddress(self, ip):
115 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
116 #self.writeNetworkConfig()
118 def setGateway(self, ip):
120 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
121 #self.writeNetworkConfig()
123 def setNetmask(self, ip):
125 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))
126 #self.writeNetworkConfig()
128 def getCurrentIP(self):
130 for x in os.popen("ifconfig eth0 | grep 'inet addr:'", "r").readline().split(' '):
131 if x.split(':')[0] == "addr":
132 ipstr = x.split(':')[1].split('.')
136 print "[Network.py] got ip " + str(ip)
142 ip = iNetwork.getCurrentIP()
145 config.network = ConfigSubsection()
146 config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, (("no", _("no")), ("yes", _("yes"))))
147 config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, configsequencearg.get("IP"))
148 config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], configsequencearg.get("IP"))
149 config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], configsequencearg.get("IP"))
150 config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], configsequencearg.get("IP"))
151 config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], configsequencearg.get("MAC"))
153 iNetwork.loadNetworkConfig()
155 #FIXME using this till other concept for this is implemented
156 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
157 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
160 def writeNetworkConfig(configElement):
161 iNetwork.writeNetworkConfig()
163 def setIPAddress(configElement):
164 iNetwork.setIPAddress(configElement.value)
166 def setGateway(configElement):
167 iNetwork.setGateway(configElement.value)
169 def setNetmask(configElement):
170 iNetwork.setNetmask(configElement.value)
172 def setDHCP(configElement):
173 iNetwork.setDHCP(configElement.value)
175 def setIPNameserver(configElement):
176 iNetwork.setIPNameserver(configElement.value)
178 def setMACAddress(configElement):
179 iNetwork.setMACAddress(configElement.value)
182 # this will call the "setup-val" initial
183 config.network.dhcp.addNotifier(setDHCP)
184 config.network.ip.addNotifier(setIPAddress)
185 config.network.netmask.addNotifier(setNetmask)
186 config.network.gateway.addNotifier(setGateway)
187 config.network.dns.addNotifier(setIPNameserver)
188 config.network.mac.addNotifier(setMACAddress)