Checking in new phone registration scripts; restructuring directories; fixing README
[lede.git] / sentinel_setup / register / version_2 / connect_device.sh
diff --git a/sentinel_setup/register/version_2/connect_device.sh b/sentinel_setup/register/version_2/connect_device.sh
new file mode 100755 (executable)
index 0000000..175799f
--- /dev/null
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+# Print usage
+if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
+       echo "This is a simple script that register connected device into the system"
+       echo "/etc/config/hostapd-psk /etc/config/dhcp will be changed accordingly"
+       echo "~/sentinel_setup/register/register_device.sh will be executed"
+       echo ""
+       echo "Usage:"
+       echo "  ./connect_device.sh [-h]"
+       echo "  ./connect_device.sh [-co <specific-pw> <device_name> <default_pw>]"
+       echo ""
+       echo "Options:"
+       echo "  -h      show this usage"
+       echo "  -co     connect a new device"
+       echo ""
+
+elif [ "$1" == "-co" ]; then
+
+       # 0
+       # Get password from $2
+       # Supposing that key for radio0 and radio1 are equal
+       PW=$2
+
+       # Save default password from $4. If the argument is empty, let's use THE default one.
+       DEFAULT="1qaz2wsx3edc"
+       if [ ! -z "$4" ]; then
+               DEFAULT=$4
+       fi
+
+       # 1
+       # Get MAC address and IP address from dhcp.leases file.
+       # Below scripts will find the most recently connected device by sorting the first column of lease file, 
+       # which is time of lease expiry, in epoch time
+       # Before that, the file named devices.dat could not exist, so touch it first
+       touch ~/sentinel_setup/register/devices.dat
+       chmod 666 ~/sentinel_setup/register/devices.dat
+
+       # now ready to use devices.dat. Get numbers of records in devices.dat
+       NR=$(cat ~/sentinel_setup/register/devices.dat | wc -l)
+
+       # use temp file for procedure so that we don't touch original lease file
+       cp /tmp/dhcp.leases leases.temp
+
+       for i in `seq 1 $NR`
+       do
+               #get line number(LN)
+               LN="${i}p"
+               #get target MAC address(TMAC) from devices.dat
+               TMAC=$(sed -n ${LN} ~/sentinel_setup/register/devices.dat | awk '{print $1}')
+               #remove the record with certain TMAC in the dhcp file so that we can get new one at the end
+               sed -e /${TMAC}/d leases.temp > leases.temp.temp
+               rm leases.temp
+               mv leases.temp.temp leases.temp
+       done
+
+       # Filter done. There should be only one line in leases.temp, but just in
+       # case sort the epoch time
+       RECENT=$(sort -nrk1,1 leases.temp | head -1)
+
+       # Get MAC, IP, NAME from RECENT
+       MAC=$(echo $RECENT | awk '{print $2}')
+       IP=$(echo $RECENT | awk '{print $3}')
+       # below was used when there were no name argument in the script
+       # NAME=$(echo $RECENT | awk '{print $4}')
+       NAME=$3
+
+       # 2
+       # Add record to the database with ~/sentinel_setup/register/register_device.sh
+       ~/sentinel_setup/register/register_device.sh -a $MAC $IP $PW $NAME
+
+       # 3
+       # rewind the default password and apply config files
+       uci commit
+       ~/sentinel_setup/register/change_default_pw.sh -ch $DEFAULT
+       /sbin/wifi
+
+else
+       echo "Unknown option. Please run ./connect_device.sh -h for usage."
+
+fi