4 if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
5 echo "This is a simple script that register connected device into the system"
6 echo "/etc/config/hostapd-psk /etc/config/dhcp will be changed accordingly"
7 echo "~/vigilia_setup/register/register_device.sh will be executed"
10 echo " ./connect_device.sh [-h]"
11 echo " ./connect_device.sh [-co <specific-pw> <MAC address> <name> <default_pw>]"
14 echo " -h show this usage"
15 echo " -co connect a new device"
18 elif [ "$1" == "-co" ]; then
21 # Get password from $2
22 # Supposing that key for radio0 and radio1 are equal
25 # Save default password from $5. If the argument is empty, let's use THE default one.
26 DEFAULT="1qaz2wsx3edc"
27 if [ ! -z "$5" ]; then
32 # Get MAC address and IP address from dhcp.leases file.
33 # Below scripts will find the most recently connected device by sorting the first column of lease file,
34 # which is time of lease expiry, in epoch time
35 # Before that, the file named devices.dat could not exist, so touch it first
36 touch ~/vigilia_setup/register/devices.dat
37 chmod 666 ~/vigilia_setup/register/devices.dat
39 # now ready to use devices.dat. Get numbers of records in devices.dat
40 NR=$(cat ~/vigilia_setup/register/devices.dat | wc -l)
42 # use temp file for procedure so that we don't touch original lease file
43 cp /tmp/dhcp.leases leases.temp
46 grep $3 leases.temp > temp.temp
48 mv temp.temp leases.temp
50 # for i in `seq 1 $NR`
52 # #get line number(LN)
54 # #get target MAC address(TMAC) from devices.dat
55 # TMAC=$(sed -n ${LN} ~/vigilia_setup/register/devices.dat | awk '{print $1}')
57 # #remove the record with certain TMAC in the dhcp file so that we can get new one at the end
58 # sed -e /${TMAC}/d leases.temp > leases.temp.temp
60 # mv leases.temp.temp leases.temp
63 # Filter done. There should be only one line in leases.temp, but just in
64 # case sort the epoch time
65 # RECENT=$(sort -nrk1,1 leases.temp | head -1)
66 # echo "Recent: $RECENT"
68 # Get MAC, IP, NAME from RECENT
69 MAC=$(awk '{print $2}' leases.temp)
70 IP=$(awk '{print $3}' leases.temp)
71 # below was used when there were no name argument in the script
72 # NAME=$(echo $RECENT | awk '{print $4}')
76 # Add record to the database with ~/vigilia_setup/register/register_device.sh
81 ~/vigilia_setup/register/register_device.sh -a $MAC $IP $PW $NAME
84 # rewind the default password and apply config files
86 ~/vigilia_setup/register/change_default_pw.sh -ch $DEFAULT
90 echo "Unknown option. Please run ./connect_device.sh -h for usage."