Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for setting up in non-interactive mode #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions initial_setup_silent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3

import argparse
import os
import sys
import setup_lib


if os.getuid():
sys.exit('You need root access to install!')

arg_parser = argparse.ArgumentParser()

arg_parser.add_argument(
"--ssid",
required=False,
default="RPI",
help="SSID for host mode")

arg_parser.add_argument(
'--auto-config-timeout',
required=False,
default=0,
type=int,
help='Switch to host mode after N seconds')

arg_parser.add_argument(
'--use-https',
required=False,
default=False,
action='store_true',
help='Use SSL for web configurator access')

arg_parser.add_argument(
'--port',
required=False,
default=80,
type=int,
help='Port for web configurator')

arg_parser.add_argument(
"--wpa-key",
required=False,
default="",
help="WPA key if decided to use WPA authentication")

arguments = arg_parser.parse_args(sys.argv[1:])

os.chdir(os.path.dirname(os.path.realpath(__file__)))

setup_lib.install_prereqs()
setup_lib.copy_configs("y" if arguments.wpa_key else "n")
setup_lib.update_main_config_file(
arguments.ssid,
"y" if arguments.auto_config_timeout else "n",
str(arguments.auto_config_timeout),
"y" if arguments.use_https else "n",
str(arguments.port),
"y" if arguments.wpa_key else "n",
arguments.wpa_key
)

15 changes: 7 additions & 8 deletions libs/configuration_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def save_credentials():
wifi_key = request.form['wifi_key']

create_wpa_supplicant(ssid, wifi_key)

# Call set_ap_client_mode() in a thread otherwise the reboot will prevent
# the response from getting to the browser
def sleep_and_start_ap():
Expand Down Expand Up @@ -116,17 +116,16 @@ def set_ap_client_mode():
def update_wpa(wpa_enabled, wpa_key):
with fileinput.FileInput('/etc/raspiwifi/raspiwifi.conf', inplace=True) as raspiwifi_conf:
for line in raspiwifi_conf:
updated = False
if 'wpa_enabled=' in line:
line_array = line.split('=')
line_array[1] = wpa_enabled
print(line_array[0] + '=' + str(line_array[1]))
print('wpa_enabled=' + str(wpa_enabled))
updated = True

if 'wpa_key=' in line:
line_array = line.split('=')
line_array[1] = wpa_key
print(line_array[0] + '=' + line_array[1])
print('wpa_key=' + wpa_key)
updated = True

if 'wpa_enabled=' not in line and 'wpa_key=' not in line:
if not updated:
print(line, end='')


Expand Down
78 changes: 39 additions & 39 deletions libs/reset_device/connection_monitor.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import time
import sys
import os
import reset_lib
no_conn_counter = 0
consecutive_active_reports = 0
config_hash = reset_lib.config_file_hash()
# If auto_config is set to 0 in /etc/raspiwifi/raspiwifi.conf exit this script
if config_hash['auto_config'] == "0":
sys.exit()
else:
# Main connection monitoring loop at 10 second interval
while True:
time.sleep(10)
# If iwconfig report no association with an AP add 10 to the "No
# Connection Couter"
if reset_lib.is_wifi_active() == False:
no_conn_counter += 10
consecutive_active_reports = 0
# If iwconfig report association with an AP add 1 to the
# consecutive_active_reports counter and 10 to the no_conn_counter
else:
consecutive_active_reports += 1
no_conn_counter += 10
# Since wpa_supplicant seems to breifly associate with an AP for
# 6-8 seconds to check the network key the below will reset the
# no_conn_counter to 0 only if two 10 second checks have come up active.
if consecutive_active_reports >= 2:
no_conn_counter = 0
consecutive_active_reports = 0
# If the number of seconds not associated with an AP is greater or
# equal to the auto_config_delay specified in the /etc/raspiwifi/raspiwifi.conf
# trigger a reset into AP Host (Configuration) mode.
if no_conn_counter >= int(config_hash['auto_config_delay']):
reset_lib.reset_to_host_mode()
import time
import sys
import os
import reset_lib

no_conn_counter = 0
consecutive_active_reports = 0
config_hash = reset_lib.config_file_hash()

# If auto_config is set to 0 in /etc/raspiwifi/raspiwifi.conf exit this script
if config_hash['auto_config'] == "0":
sys.exit()
else:
# Main connection monitoring loop at 10 second interval
while True:
time.sleep(10)

# If iwconfig report no association with an AP add 10 to the "No
# Connection Couter"
if reset_lib.is_wifi_active() == False:
no_conn_counter += 10
consecutive_active_reports = 0
# If iwconfig report association with an AP add 1 to the
# consecutive_active_reports counter and 10 to the no_conn_counter
else:
consecutive_active_reports += 1
no_conn_counter += 10
# Since wpa_supplicant seems to breifly associate with an AP for
# 6-8 seconds to check the network key the below will reset the
# no_conn_counter to 0 only if two 10 second checks have come up active.
if consecutive_active_reports >= 2:
no_conn_counter = 0
consecutive_active_reports = 0

# If the number of seconds not associated with an AP is greater or
# equal to the auto_config_delay specified in the /etc/raspiwifi/raspiwifi.conf
# trigger a reset into AP Host (Configuration) mode.
if no_conn_counter >= int(config_hash['auto_config_delay']):
reset_lib.reset_to_host_mode()
18 changes: 13 additions & 5 deletions libs/reset_device/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@
import subprocess
import reset_lib

def get_serial():
serial = "UNKN"
with open("/sys/firmware/devicetree/base/serial-number") as f:
serial = f.read()
return serial.replace("\x00", "")


GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

counter = 0
serial_last_four = subprocess.check_output(['cat', '/proc/cpuinfo'])[-5:-1].decode('utf-8')
serial_last_four = get_serial()[-4:]
config_hash = reset_lib.config_file_hash()
ssid_prefix = config_hash['ssid_prefix'] + " "
reboot_required = False

wpa_changed = reset_lib.wpa_check_activate(config_hash['wpa_enabled'] == "1", config_hash['wpa_key'])

reboot_required = reset_lib.wpa_check_activate(config_hash['wpa_enabled'], config_hash['wpa_key'])
ssid_changed = reset_lib.update_ssid(ssid_prefix, serial_last_four)

reboot_required = reset_lib.update_ssid(ssid_prefix, serial_last_four)
reboot_required = wpa_changed or ssid_changed

if reboot_required == True:
if reboot_required:
print("Rebooting, wpa_changed = {}, ssid_changed = {}", wpa_changed, ssid_changed)
os.system('reboot')

# This is the main logic loop waiting for a button to be pressed on GPIO 18 for 10 seconds.
Expand Down
181 changes: 88 additions & 93 deletions libs/reset_device/reset_lib.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,88 @@
import os
import fileinput
import subprocess

def config_file_hash():
config_file = open('/etc/raspiwifi/raspiwifi.conf')
config_hash = {}

for line in config_file:
line_key = line.split("=")[0]
line_value = line.split("=")[1].rstrip()
config_hash[line_key] = line_value

return config_hash

def wpa_check_activate(wpa_enabled, wpa_key):
wpa_active = False
reboot_required = False

with open('/etc/hostapd/hostapd.conf') as hostapd_conf:
for line in hostapd_conf:
if 'wpa_passphrase' in line:
wpa_active = True

if wpa_enabled == '1' and wpa_active == False:
reboot_required = True
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.wpa /etc/hostapd/hostapd.conf')

if wpa_enabled == '1':
with fileinput.FileInput('/etc/hostapd/hostapd.conf', inplace=True) as hostapd_conf:
for line in hostapd_conf:
if 'wpa_passphrase' in line:
if 'wpa_passphrase=' + wpa_key not in line:
print('wpa_passphrase=' + wpa_key)
os.system('reboot')
else:
print(line, end = '')
else:
print(line, end = '')

if wpa_enabled == '0' and wpa_active == True:
reboot_required = True
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.nowpa /etc/hostapd/hostapd.conf')

return reboot_required

def update_ssid(ssid_prefix, serial_last_four):
reboot_required = False
ssid_correct = False

with open('/etc/hostapd/hostapd.conf') as hostapd_conf:
for line in hostapd_conf:
if ssid_prefix in line:
ssid_correct = True

if ssid_correct == False:
with fileinput.FileInput("/etc/hostapd/hostapd.conf", inplace=True) as file:
for line in file:
if 'ssid=' in line:
line_array = line.split('=')
line_array[1] = ssid_prefix + ' ' + serial_last_four
print(line_array[0] + '=' + line_array[1])
else:
print(line, end = '')

reboot_required = True

return reboot_required

def is_wifi_active():
iwconfig_out = subprocess.check_output(['iwconfig']).decode('utf-8')
wifi_active = True

if "Access Point: Not-Associated" in iwconfig_out:
wifi_active = False

return wifi_active

def reset_to_host_mode():
if not os.path.isfile('/etc/raspiwifi/host_mode'):
os.system('aplay /usr/lib/raspiwifi/reset_device/button_chime.wav')
os.system('rm -f /etc/wpa_supplicant/wpa_supplicant.conf')
os.system('rm -f /home/pi/Projects/RaspiWifi/tmp/*')
os.system('rm /etc/cron.raspiwifi/apclient_bootstrapper')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/aphost_bootstrapper /etc/cron.raspiwifi/')
os.system('chmod +x /etc/cron.raspiwifi/aphost_bootstrapper')
os.system('mv /etc/dhcpcd.conf /etc/dhcpcd.conf.original')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/dhcpcd.conf /etc/')
os.system('mv /etc/dnsmasq.conf /etc/dnsmasq.conf.original')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/dnsmasq.conf /etc/')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/dhcpcd.conf /etc/')
os.system('touch /etc/raspiwifi/host_mode')
os.system('reboot')
import os
import fileinput
import subprocess

def config_file_hash():
config_file = open('/etc/raspiwifi/raspiwifi.conf')
config_hash = {}

for line in config_file:
line_key = line.split("=")[0]
line_value = line.split("=")[1].rstrip()
config_hash[line_key] = line_value

return config_hash

def wpa_check_activate(wpa_enabled, wpa_key):
wpa_active = False
reboot_required = False

with open('/etc/hostapd/hostapd.conf') as hostapd_conf:
for line in hostapd_conf:
if 'wpa_passphrase' in line:
wpa_active = True

if wpa_enabled and not wpa_active:
reboot_required = True
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.wpa /etc/hostapd/hostapd.conf')

if not wpa_enabled and wpa_active:
reboot_required = True
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.nowpa /etc/hostapd/hostapd.conf')

if wpa_enabled:
with fileinput.FileInput('/etc/hostapd/hostapd.conf', inplace=True) as hostapd_conf:
for line in hostapd_conf:
if 'wpa_passphrase=' + wpa_key not in line:
print('wpa_passphrase=' + wpa_key)
else:
print(line, end = '')

return reboot_required

def update_ssid(ssid_prefix, serial_last_four):
reboot_required = False
ssid_correct = False
ssid = ssid_prefix + "_" + serial_last_four

with open('/etc/hostapd/hostapd.conf') as hostapd_conf:
for line in hostapd_conf:
if ssid in line:
ssid_correct = True

if ssid_correct == False:
with fileinput.FileInput("/etc/hostapd/hostapd.conf", inplace=True) as file:
for line in file:
if 'ssid=' in line:
print('ssid=' + ssid)
else:
print(line, end = '')

reboot_required = True

return reboot_required

def is_wifi_active():
iwconfig_out = subprocess.check_output(['iwconfig']).decode('utf-8')
wifi_active = True

if "Access Point: Not-Associated" in iwconfig_out:
wifi_active = False

return wifi_active

def reset_to_host_mode():
if not os.path.isfile('/etc/raspiwifi/host_mode'):
os.system('aplay /usr/lib/raspiwifi/reset_device/button_chime.wav')
os.system('rm -f /etc/wpa_supplicant/wpa_supplicant.conf')
os.system('rm -f /home/pi/Projects/RaspiWifi/tmp/*')
os.system('rm /etc/cron.raspiwifi/apclient_bootstrapper')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/aphost_bootstrapper /etc/cron.raspiwifi/')
os.system('chmod +x /etc/cron.raspiwifi/aphost_bootstrapper')
os.system('mv /etc/dhcpcd.conf /etc/dhcpcd.conf.original')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/dhcpcd.conf /etc/')
os.system('mv /etc/dnsmasq.conf /etc/dnsmasq.conf.original')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/dnsmasq.conf /etc/')
os.system('cp /usr/lib/raspiwifi/reset_device/static_files/dhcpcd.conf /etc/')
os.system('touch /etc/raspiwifi/host_mode')
os.system('reboot')
Loading