X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=vigilia_setup%2Fregister%2Fversion_2%2Fconnect_device.sh;fp=vigilia_setup%2Fregister%2Fversion_2%2Fconnect_device.sh;h=175799f2d4a4b7991c301c3ab5ee6da2a6e0a15b;hb=7ff6b5c63dab61f55310ca9f99a079c5b0627be3;hp=0000000000000000000000000000000000000000;hpb=7de6013839a457b5735d7961b994cce0976747f3;p=lede.git diff --git a/vigilia_setup/register/version_2/connect_device.sh b/vigilia_setup/register/version_2/connect_device.sh new file mode 100755 index 0000000000..175799f2d4 --- /dev/null +++ b/vigilia_setup/register/version_2/connect_device.sh @@ -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 ]" + 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