forked from learningequality/ka-lite-config-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure_network_interfaces.py
More file actions
executable file
·60 lines (48 loc) · 1.72 KB
/
configure_network_interfaces.py
File metadata and controls
executable file
·60 lines (48 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# this script modifies the network interface config file to support AP mode
import os, sys
# make sure we have write access to the network interface config file
if not os.access("/etc/network/interfaces", os.W_OK):
print "No write access to /etc/network/interfaces -- aborting! (must be run as root or with sudo)"
sys.exit(1)
interface_file = open("/etc/network/interfaces", "r")
interface_lines = interface_file.readlines()
interface_file.close()
output_lines = []
# certain lines need commenting
lines_to_comment_out = [
"allow-hotplug",
"wpa-roam",
"iface default",
]
# certain lines need inserting
wlan0_lines = [
"",
"iface wlan0 inet static",
" address 172.27.0.1",
" netmask 255.255.255.0",
" pre-up sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp -j DNAT --to-destination 172.27.0.1",
]
editing_wlan0 = False
for line in interface_lines:
if any(line.startswith(beginning) for beginning in lines_to_comment_out):
line = "#" + line
# start deleting lines where the wlan0 spec starts
if line.startswith("iface wlan0 inet"):
editing_wlan0 = True
line = None
elif editing_wlan0: # keep deleting until indented block ends
if line.startswith(" "):
line = None
else:
editing_wlan0 = False
# add the line to the output list
if line is not None:
output_lines.append(line.rstrip())
# stick the wlan0 spec onto the end of the output list
output_lines += wlan0_lines
# join, and remove double rows of empty lines
output = "\n".join(output_lines).replace("\n\n\n", "\n\n")
# write to file
interface_file = open("/etc/network/interfaces", "w")
interface_lines = interface_file.write(output)
interface_file.close()