get ip address from environment instead of using config file
[enigma2.git] / lib / python / Components / Network.py
1 from config import *
2
3 import os
4 from socket import *
5
6 class Network:
7         def __init__(self):
8                 pass
9                 
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.tmp', 'w')
14                 fp.write("auto eth0\n")
15                 if (config.network.dhcp.value == "yes"):
16                         fp.write("iface eth0 inet dhcp\n")
17                 else:
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))
22                 fp.close()
23
24         def activateNetworkConfig(self):
25                 import os
26                 #os.system("/etc/init.d/networking restart")
27                 
28         def setDHCP(self, useDHCP):
29                 if (useDHCP):
30                         print "Using DHCP"
31                         config.network.ip.enabled = False
32                         config.network.netmask.enabled = False
33                         config.network.gateway.enabled = False
34                         config.network.dns.enabled = False
35                 else:
36                         print "NOT using DHCP"
37                         config.network.ip.enabled = True
38                         config.network.netmask.enabled = True
39                         config.network.gateway.enabled = True
40                         config.network.dns.enabled = True
41                                         
42         def setIPNameserver(self, ip):
43                 return
44                 resolvconf = file('/etc/resolv.conf', 'w')
45                 resolvconf.write("nameserver %d.%d.%d.%d" % tuple(ip))
46                 resolvconf.close()
47                 
48         def setMACAddress(self, mac):
49                 #os.system("echo ifconfig eth0 ether %02x:%02x:%02x:%02x:%02x:%02x" % tuple(mac))
50                 pass
51                 
52         def setIPAddress(self, ip):
53                 os.system("echo ifconfig eth0 %d.%d.%d.%d" % tuple(ip))
54                 self.writeNetworkConfig()
55
56         def setGateway(self, ip):
57                 os.system("echo route add default gw %d.%d.%d.%d" % tuple(ip))
58                 self.writeNetworkConfig()
59                 
60         def setNetmask(self, ip):
61                 os.system("echo ifconfig eth0 netmask %d.%d.%d.%d" % tuple(ip))         
62                 self.writeNetworkConfig()               
63
64 def InitNetwork():
65         ipstr = gethostbyname(gethostname()).split('.')
66         ip = []
67         for i in ipstr:
68                 ip.append(int(i))
69
70         config.network = ConfigSubsection()
71         config.network.dhcp = configElement_nonSave("config.network.dhcp", configSelection, 1, ("no", "yes"))
72         config.network.ip = configElement_nonSave("config.network.ip", configSequence, ip, (("."), (1,255)))
73         config.network.netmask = configElement_nonSave("config.network.netmask", configSequence, [255,255,255,0], (("."), (1,255)))
74         config.network.gateway = configElement_nonSave("config.network.gateway", configSequence, [192,168,1,3], (("."), (1,255)))
75         config.network.dns = configElement_nonSave("config.network.dns", configSequence, [192,168,1,3], (("."), (1,255)))
76         config.network.mac = configElement_nonSave("config.network.mac", configSequence, [00,11,22,33,44,55], ((":"), (1,255)))
77         
78         #FIXME using this till other concept for this is implemented
79         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes, sir", "you are my hero"))
80         #config.network.activate = configElement("config.network.activate", configSelection, 0, ("yes", "you are my hero"))
81
82         iNetwork = Network()
83
84         def writeNetworkConfig(configElement):
85                 iNetwork.writeNetworkConfig()
86                 
87         def setIPAddress(configElement):
88                 iNetwork.setIPAddress(configElement.value)
89
90         def setGateway(configElement):
91                 iNetwork.setGateway(configElement.value)
92
93         def setNetmask(configElement):
94                 iNetwork.setNetmask(configElement.value)                
95
96         def setDHCP(configElement):
97                 iNetwork.setDHCP(configElement.value)
98
99         def setIPNameserver(configElement):
100                 iNetwork.setIPNameserver(configElement.value)
101
102         def setMACAddress(configElement):
103                 iNetwork.setMACAddress(configElement.value)
104
105
106         # this will call the "setup-val" initial
107         config.network.dhcp.addNotifier(setDHCP)
108         config.network.ip.addNotifier(setIPAddress)
109         config.network.netmask.addNotifier(setNetmask)  
110         config.network.gateway.addNotifier(setGateway)
111         config.network.dns.addNotifier(setIPNameserver)
112         config.network.mac.addNotifier(setMACAddress)