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()
32 split = i.strip().split(' ')
33 if (split[0] == "iface"):
36 if (len(split) == 4 and split[3] == "dhcp"):
37 ifaces[currif]["dhcp"] = "yes"
39 ifaces[currif]["dhcp"] = "no"
41 if (split[0] == "address"):
42 ifaces[currif]["address"] = map(int, split[1].split('.'))
43 if (split[0] == "netmask"):
44 ifaces[currif]["netmask"] = map(int, split[1].split('.'))
45 if (split[0] == "gateway"):
46 ifaces[currif]["gateway"] = map(int, split[1].split('.'))
49 if (ifaces.has_key("eth0")):
50 if (ifaces["eth0"]["dhcp"] == "yes"):
51 config.network.dhcp.value = 1
53 config.network.dhcp.value = 0
54 if (ifaces["eth0"].has_key("address")): config.network.ip.value = ifaces["eth0"]["address"]
55 if (ifaces["eth0"].has_key("netmask")): config.network.netmask.value = ifaces["eth0"]["netmask"]
56 if (ifaces["eth0"].has_key("gateway")): config.network.gateway.value = ifaces["eth0"]["gateway"]
61 def activateNetworkConfig(self):
63 os.system("/etc/init.d/networking restart")
65 def setDHCP(self, useDHCP):
68 config.network.ip.enabled = False
69 config.network.netmask.enabled = False
70 config.network.gateway.enabled = False
71 config.network.dns.enabled = False
73 print "NOT using DHCP"
74 config.network.ip.enabled = True
75 config.network.netmask.enabled = True
76 config.network.gateway.enabled = True
77 config.network.dns.enabled = True
79 def setIPNameserver(self, ip):
81 resolvconf = file('/etc/resolv.conf', 'w')
82 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
85 def setMACAddress(self, mac):
86 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
89 def setIPAddress(self, ip):
91 #os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
92 #self.writeNetworkConfig()
94 def setGateway(self, ip):
96 #os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
97 #self.writeNetworkConfig()
99 def setNetmask(self, ip):
101 #os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))
102 #self.writeNetworkConfig()
108 ip = map (int, gethostbyname(gethostname()).split('.'))
110 config.network = ConfigSubsection()
111 config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, ("no", "yes"))
112 config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, (("."), (1,255)))
113 config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], (("."), (1,255)))
114 config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], (("."), (1,255)))
115 config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], (("."), (1,255)))
116 config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], ((":"), (1,255)))
118 iNetwork.loadNetworkConfig()
120 #FIXME using this till other concept for this is implemented
121 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
122 #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
125 def writeNetworkConfig(configElement):
126 iNetwork.writeNetworkConfig()
128 def setIPAddress(configElement):
129 iNetwork.setIPAddress(configElement.value)
131 def setGateway(configElement):
132 iNetwork.setGateway(configElement.value)
134 def setNetmask(configElement):
135 iNetwork.setNetmask(configElement.value)
137 def setDHCP(configElement):
138 iNetwork.setDHCP(configElement.value)
140 def setIPNameserver(configElement):
141 iNetwork.setIPNameserver(configElement.value)
143 def setMACAddress(configElement):
144 iNetwork.setMACAddress(configElement.value)
147 # this will call the "setup-val" initial
148 config.network.dhcp.addNotifier(setDHCP)
149 config.network.ip.addNotifier(setIPAddress)
150 config.network.netmask.addNotifier(setNetmask)
151 config.network.gateway.addNotifier(setGateway)
152 config.network.dns.addNotifier(setIPNameserver)
153 config.network.mac.addNotifier(setMACAddress)