Setup the SD card for WiFi on the radxa zero 3w for use with gphoto2 to upload photos
In this part we are going to create some scripts to safely store your ssid plus password on the SD card.
Table of contents
Why and what
As I want something like the same functionality as the Eye-Fi offered, one of those things was to store a number of ssid/password combinations and it automatically connects to the known ssid.
The ssid/password combinations are copied from the SD card and stored on the eMMC.
For the windows users only
We are going to use openssl to encrypt and decrypt the password, this command is almost always available on Linux and if it is not available, it is in the distribution repositories and can be installed from there.
On windows however it is not that easy, the easiest way I found was to install git from git for windows. To run the command, run git-bash, this will open a new window and there the openssl command is available.
UML Sequence diagram
What the after mount script should do. Basically it should configure:
- the ssid’s
- ?ap mode?
- turn on/off ssh
- ?turn on/off vpn?
- ?set scp destination?
- ?configure gphoto2 actions?
- what other things you can think off
The script
The script can be found on github. (a working copy will be posted here)
#! /bin/bash
echo "Done mounting" > /root/done_mounting
mkdir -p ~/keys
if [ ! -f "/root/keys/pwdPrivKey.pem" ]; then
openssl genrsa -out /root/keys/pwdPrivKey.pem 2048
openssl rsa -in /root/keys/pwdPrivKey.pem -out /root/keys/pwdPubKey.pem -outform PEM -pubout
chmod 0600 /root/keys/pwdPrivKey.pem
fi
mkdir -p /mnt/sdcard/keys
if [ ! -f /mnt/sdcard/keys/pwdPubKey.pem ]; then
cp ~/keys/pwdPubKey.pem /mnt/sdcard/keys/pwdPubKey.pem
#example for encripting the password
echo 'echo "password" | openssl rsautl -encrypt -inkey /mnt/sdcard/keys/pwdPubKey.pem -pubin -out pwd.dat' > /mnt/sdcard/encrypt-pass\
word-example.sh
chmod +x /mnt/sdcard/encrypt-password-example.sh
fi
old_ifs="$IFS"
IFS='
'
Cardssids=()
Cardpwds=()
# get ssid's on the card
for ssid in `ls /mnt/sdcard/ssids`; do
Cardssids+=( $ssid )
password=$(openssl rsautl -decrypt -inkey keys/pwdPrivKey.pem -in /mnt/sdcard/ssids/$ssid)
Cardpwds+=( $password )
done
nmssids=()
for ssid in `nmcli -f name c show| grep -v NAME | sort | uniq`; do
nmssids+=( $(echo $ssid | tr -d " ") )
done
# do some cross checking
for i in ${!Cardssids[@]}; do
for j in ${!nmssids[@]}; do
if [ "${Cardssids[$i]}" == "${nmssids[$j]}" ] ; then
# remove from both lists
unset "nmssids[$j]"
unset "Cardssids[$i]"
unset "Cardpwds[$i]"
fi
done
done
#refresh ssid scan, somehow this is needed
nmcli dev wifi >/dev/null
# remove ssids not in the Cardsssids list from the networkmanager
for j in ${!nmssids[@]}; do
echo "remove from nm:" \"${nmssids[$j]}\"
nmcli c delete "${nmssids[$j]}"
done
# add to network manager list
for j in ${!Cardssids[@]}; do
echo "add to nm:" \"${Cardssids[$j]}\"
nmcli dev wifi connect "${Cardssids[$j]}" password "${Cardpwds[$j]}"
done
IFS=$old_ifs
nmcli -f ssid dev wifi | grep -v SSID| grep -v "\-\-"|sort | uniq > /mnt/sdcard/ssid-list
# Do sshd check
if [ `cat /mnt/sdcard/sshd` == 1 ] ; then
systemctl start ssh
systemctl enable ssh
else
systemctl stop ssh
systemctl disable ssh
fi
systemctl status ssh > /mnt/sdcard/sshd-status
(place holder)