This repository was archived by the owner on May 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpi_initial_setup.py
executable file
·320 lines (253 loc) · 10.8 KB
/
pi_initial_setup.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
import argparse
import getpass
import os
import shutil
import subprocess
import time
import sys
import pi_settings as settings
# Obtain absolute location of this python file
def file_directory():
path = os.path.dirname(os.path.realpath(__file__))
return path
# Obtain command line arguments
def fetch_argument():
parser = argparse.ArgumentParser(description='Initial Pi Setup')
parser.add_argument('-d', '--nodns',
help='Skip configuring dynamic dns. Users are expected to configure their domain name as they '
'see fit.',
action='store_true'
)
parser.add_argument('-m', '--manual',
help='Script does not configure the network for internet connection for eth1. \n'
'User expected to configure the network themselves for eth1',
action='store_true'
)
input_arguments = parser.parse_args()
return input_arguments
def user_prompt(prompt_message):
while True:
sys.stdout.write(prompt_message + '? [y/n] ')
valid_choices = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
choice = input().lower()
if choice in valid_choices:
break
else:
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
return valid_choices[choice]
# Check if wireless or wired connection and fetch parameters accordingly
def fetch_wireless_parameters():
if user_prompt('Are you connecting the raspberry pi to a wireless internet connection'):
while True:
sys.stdout.write('Enter the Wireless network SSID : ')
wifi_ssid = input()
if len(wifi_ssid):
break
else:
print('[ERROR] Please enter an Wireless network SSID')
else:
print('Note: The wired internet connection MUST be through the ethernet adapter connected to the USB port')
return None, None, None
wifi_username = None
# WPA-EAP requires an username and password for connecting to the network. Typical enterprise config
# WPA-PSK requires only a password (pre-shared key) for connecting to network. Typical home network
if user_prompt('Are you trying to connect to an WPA-Enterprise which requires an username for connection'):
while True:
sys.stdout.write('Enter the Username required for connecting to the Wireless network : ')
wifi_username = input()
if len(wifi_username):
break
else:
print('[ERROR] Please enter an Username for authenticating with the Wireless network')
while True:
wifi_password = getpass.getpass('Enter Wifi Password : ')
wifi_verify_password = getpass.getpass('Re-enter Wifi Password : ')
if wifi_password == wifi_verify_password:
break
else:
print("[Error] The passwords do not match. Please enter again.")
return wifi_ssid, wifi_username, wifi_password
# Configurations directly modifying pi
def pi_configuration():
# Pi for a headless application then you can reduce the memory split
# between the GPU and the rest of the system down to 16mb
print('Setting GPU memory to 16mb')
config_file = '/boot/config.txt'
try:
settings.backup_file(config_file)
except PermissionError:
print("[ERROR] Code is executed as a non privileged user."
"\n[ERROR] Please re-run the script as superuser. [ sudo ./{} ]".format(os.path.basename(__file__)))
sys.exit()
with open(config_file, 'a') as file_object:
file_object.write('gpu_mem=16')
# Forcing user to change default pi password
print('Please change the default Rapberry Pi password')
while True:
try:
subprocess.check_output('passwd pi', shell=True)
except subprocess.CalledProcessError:
print("[ERROR] Please try again!")
continue
break
# Creating a file called ssh in boot.
# This is required to enable ssh connection to pi
with open('/boot/ssh', 'w') as file_object:
file_object.write('')
# Changing default keyboard layout to 'US'
subprocess.check_output(['sed', '-i', '--',
's|pc105|pc104|g',
'/etc/default/keyboard'])
subprocess.check_output(['sed', '-i', '--',
's|gb|us|g',
'/etc/default/keyboard'])
# Create the firewall configuration for the raspberry pi
def firewall_configuration(base_path):
print('Setting up the firewall configuration')
firewall_path = '/etc/firewall'
firewall_script_name = '/iptables.sh'
# Creating the folder for our firewall script file
try:
os.makedirs(firewall_path)
except OSError as error:
if 'File exists' in error.strerror:
print('{} directory already exists'.format(firewall_path))
# copying iptables rules to the new folder
shutil.copy2(base_path + firewall_script_name, firewall_path + firewall_script_name)
# Changing file permissions
os.chmod(firewall_path + firewall_script_name, 0o700)
subprocess.check_output(['chown', 'root', firewall_path + firewall_script_name])
# Create the dynamic dns configuration for the raspberry pi
def dns_configuration(base_path, wireless):
print('Setting up the dynamic dns configuration')
file_name = '/dynv6.sh'
path_name = '/etc/dns'
token_file_name = '/dynv6_token.txt'
if wireless is None:
device = 'eth1'
else:
device = 'wlan0'
# Creating the folder for our dns script file
try:
os.makedirs(path_name)
except OSError as error:
if 'File exists' in error.strerror:
print('{} directory already exists'.format(path_name))
# copying dns script to the new folder
shutil.copy2(base_path + file_name, path_name + file_name)
# Changing file permissions
os.chmod(path_name + file_name, 0o700)
subprocess.check_output(['chown', 'root', path_name + file_name])
with open(base_path + token_file_name, 'r') as file_object:
data = file_object.readlines()
dns_token = None
for string in data:
if len(string) > 0:
if string.strip()[0] != '#' and 'token' in string.strip():
dns_token = string.strip().split('=')[1].strip()
if dns_token is None or dns_token is 'TOKEN_WILL_REPLACE_THIS':
print(('[ERROR] The file {} does not have a valid dynv6_token.\n'
'Check out https://dynv6.com/docs/apis for token.\n'
'Then token must be present in the file of the form "token = YOUR TOKEN"\n'
).format(file_name))
sys.exit()
subprocess.check_output(['sed', '-i', '--',
's|token="YOUR_DYNV6_TOKEN_HERE"|token="' + dns_token + '"|g',
path_name + file_name])
subprocess.check_output(['sed', '-i', '--',
's|hostname="YOUR_DOMAIN_NAME_HERE"|hostname="' + settings.DOMAIN_NAME + '"|g',
path_name + file_name])
subprocess.check_output(['sed', '-i', '--',
's|device="YOUR_NETWORK_DEVICE_NAME_HERE"|device="' + device + '"|g',
path_name + file_name])
# Create Wifi configuration for connecting to Wireless network or not if wired.
# Creates the interface configuration for the scientific instrument.
def network_configuration(wifi_ssid, wpa_username, wpa_password, no_dynamic_dns, manual_config):
print('Setting up the internet configuration')
# For wifi configuration
wpa_config_file = '/etc/wpa_supplicant/wpa_supplicant.conf'
interfaces_file = '/etc/network/interfaces'
loopback_config = (
'\nauto lo\n'
'iface lo inet loopback\n'
)
ethernet_config_instrument = (
'\nauto eth0\n'
'iface eth0 inet static\n'
'\taddress 192.168.7.1\n'
'\tnetmask 255.255.255.0\n'
'\tnetwork 192.168.7.0\n\n'
)
# Taking a backup of interfaces file
settings.backup_file(interfaces_file)
ethernet_config_internet = (
'\nauto eth1\n'
'iface eth1 inet dhcp\n'
'iface eth1 inet6 dhcp\n'
)
# Wired internet connection
if wifi_ssid is None:
write_values = loopback_config + ethernet_config_instrument
if not manual_config:
write_values = write_values + ethernet_config_internet
with open(interfaces_file, 'a') as file_object:
file_object.write(write_values)
return
# Wireless internet connection
if wpa_username is not None:
# WPA-EAP Configuration
wpa_config = (
'\tssid="{}"\n'
'\tkey_mgmt=WPA-EAP\n'
'\tpairwise=CCMP TKIP\n'
'\tgroup=CCMP TKIP\n'
'\teap=PEAP\n'
'\tphase1="peapver=0"\n'
'\tphase2="MSCHAPV2"\n'
'\tidentity="{}"\n'
'\tpassword="{}"\n'
).format(wifi_ssid, wpa_username, wpa_password)
else:
# WPA-PSK Configuration
# Note this configuration has not been (and won't be) tested.
wpa_config = (
'\tssid="{}"\n'
'\tpsk="{}"\n'
).format(wifi_ssid, wpa_password)
final_wpa_config = '\nnetwork={\n' + wpa_config + '}\n'
wifi_config_list = [
'\nauto wlan0\n',
'allow-hotplug wlan0\n',
'iface wlan0 inet dhcp\n',
'\twpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n'
# '\tpre-up /bin/bash /etc/firewall/iptables.sh\n'
]
if not no_dynamic_dns:
wifi_config_list.append('\tpost-up /bin/bash /etc/dns/dynv6.sh\n')
print('Adding WPA configuration to {} file'.format(wpa_config_file))
settings.backup_file(wpa_config_file)
with open(wpa_config_file, 'a') as file_object:
file_object.write(final_wpa_config)
print('Adding WIFI configuration to {} file'.format(interfaces_file))
with open(interfaces_file, 'a') as file_object:
file_object.write(loopback_config + ethernet_config_instrument + ''.join(wifi_config_list))
# Rebooting the raspberry pi
def clean_up_setup():
print('Rebooting the system in 5 seconds...')
time.sleep(5)
subprocess.check_output(['reboot', 'now'])
if __name__ == '__main__':
arguments = fetch_argument()
no_dns = arguments.nodns
manual = arguments.manual
settings.test_values()
base_directory = file_directory()
ssid, username, password = fetch_wireless_parameters()
pi_configuration()
firewall_configuration(base_directory)
if not no_dns:
dns_configuration(base_directory, ssid)
network_configuration(ssid, username, password, no_dns, manual)
clean_up_setup()