Add auto config of wifi using a usb disk with 'wifi.txt' config file

This commit is contained in:
KemoNine 2020-08-18 18:58:23 -04:00
parent f1046c8877
commit a243b2c8fb
4 changed files with 87 additions and 1 deletions

View file

@ -14,6 +14,7 @@ Items marked ```REQUIRED``` are assumed to be setup and working. You've been war
* [Finalize Setup (REQUIRED)](finalize_setup.md) * [Finalize Setup (REQUIRED)](finalize_setup.md)
* [Setup swap (REQUIRED)](swap.md) * [Setup swap (REQUIRED)](swap.md)
* [Additional Networking Setup (REQUIRED)](networking.md) * [Additional Networking Setup (REQUIRED)](networking.md)
* [Automatic WiFi Setup (VERY SMART)](auto_wifi_setup.md)
* [Apt use RAM (VERY SMART FOR 4Gb RAM)](apt_cache_tmpfs.md) * [Apt use RAM (VERY SMART FOR 4Gb RAM)](apt_cache_tmpfs.md)
* [Automatic Updates (VERY SMART)](auto_updates.md) * [Automatic Updates (VERY SMART)](auto_updates.md)
* [Logs in RAM (VERY SMART)](log2ram.md) * [Logs in RAM (VERY SMART)](log2ram.md)

29
docs/auto_wifi_setup.md Normal file
View file

@ -0,0 +1,29 @@
# Automatic WiFi Config
The below will setup automatic WiFi configuration via text file. The way this will work is you plug in a USB disk with a file named ```wifi.txt``` with the ```essid``` as the first line and ```password``` as the second line in the file. On boot a small script will run that reads the file and re-configures the PiFrame's WiFi to use the new values.
Can be used for setup and/or re-configuring WiFi if something changes.
``` sh
git clone https://git.kemonine.info/PiFrame/piframe.git /opt/piframe
cp /opt/piframe/wifi_setup.py /usr/local/bin
chmod a+x /usr/local/bin/wifi_setup.py
cat > /etc/systemd/system/wifi_setup.service <<EOF
[Unit]
Description=Automatic configuration of WiFi on boot
[Service]
User=root
PrivateTmp=true
ExecStart=/usr/local/bin/wifi_setup.py
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now wifi_setup
```

View file

@ -26,7 +26,7 @@ systemctl enable --now firewalld
# Setup Ethernet # Setup Ethernet
# Setup by the above netplan tweaks # Setup by the above netplan tweaks
# Setup Wifi (optional) # Setup Wifi via CLI (optional)
nmtui nmtui
``` ```

56
wifi_setup.py Normal file
View file

@ -0,0 +1,56 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import subprocess
# Make sure mount point exists
if not os.path.isdir('/var/wifi'):
os.mkdir('/var/wifi')
# Scan unmounted disks/partitions to see if we need to reconfig wifi
blkids = subprocess.run(['/usr/sbin/blkid', '-o', 'device'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
for blkid in blkids.stdout.split('\n'):
blkid = blkid.strip()
if not blkid:
continue
# Verify the disk/partition isn't mounted
findmnt = subprocess.run(['/usr/bin/findmnt', '-n', '-o', 'TARGET', blkid], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
mnt = findmnt.stdout.strip()
if not mnt and not 'zram' in blkid:
print('checking ' + blkid)
try:
# Mount the disk
subprocess.run(['/usr/bin/mount', blkid, '/var/wifi/'], universal_newlines=True)
# Check if the wifi.txt confg file is present (line 1 is essid, line 2 is PSK)
if os.path.exists('/var/wifi/wifi.txt'):
# Get wifi config
essid = ''
password = ''
with open('/var/wifi/wifi.txt', 'r') as f:
essid = f.readline().strip()
password = f.readline().strip()
if essid and password:
print('Found wifi config (' + essid + ' / ' + password + ')')
# Look for existing wifi connections that should be cleaned up
nmcli = subprocess.run(['/usr/bin/nmcli', '-t', 'connection', 'show'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
for connection in nmcli.stdout.split('\n'):
# Ignore empty lines in output
if not connection:
continue
connection_details = connection.split(':')
if connection_details[2] == '802-11-wireless':
# If we cannot clean up a wifi connection assume it's a non-issue
try:
# Cleanup existing wifi connection
print('cleaning up wifi connection ' + connection_details[0])
subprocess.run(['/usr/bin/nmcli', 'connection', 'del', connection_details[1]], universal_newlines=True)
except:
pass
print('Connecting to ' + essid + ' with password ' + password)
subprocess.run(['/usr/bin/nmcli', 'd', 'wifi', 'connect', essid, 'password', password], universal_newlines=True)
# Unmount disk so it's safe to remove
subprocess.run(['/usr/bin/umount', '/var/wifi/'], universal_newlines=True)
except:
# Unmount disk so it's safe to remove
subprocess.run(['/usr/bin/umount', '/var/wifi/'], universal_newlines=True)