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 "~/sentinel_setup/register/register_device.sh will be executed"
10 echo " ./connect_device.sh [-h]"
11 echo " ./connect_device.sh [-co <specific-pw> <device_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 $4. If the argument is empty, let's use THE default one.
26 DEFAULT="1qaz2wsx3edc"
27 if [ ! -z "$4" ]; 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 ~/sentinel_setup/register/devices.dat
37 chmod 666 ~/sentinel_setup/register/devices.dat
39 # now ready to use devices.dat. Get numbers of records in devices.dat
40 NR=$(cat ~/sentinel_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
49 #get target MAC address(TMAC) from devices.dat
50 TMAC=$(sed -n ${LN} ~/sentinel_setup/register/devices.dat | awk '{print $1}')
51 #remove the record with certain TMAC in the dhcp file so that we can get new one at the end
52 sed -e /${TMAC}/d leases.temp > leases.temp.temp
54 mv leases.temp.temp leases.temp
57 # Filter done. There should be only one line in leases.temp, but just in
58 # case sort the epoch time
59 RECENT=$(sort -nrk1,1 leases.temp | head -1)
61 # Get MAC, IP, NAME from RECENT
62 MAC=$(echo $RECENT | awk '{print $2}')
63 IP=$(echo $RECENT | awk '{print $3}')
64 # below was used when there were no name argument in the script
65 # NAME=$(echo $RECENT | awk '{print $4}')
69 # Add record to the database with ~/sentinel_setup/register/register_device.sh
70 ~/sentinel_setup/register/register_device.sh -a $MAC $IP $PW $NAME
73 # rewind the default password and apply config files
75 ~/sentinel_setup/register/change_default_pw.sh -ch $DEFAULT
79 echo "Unknown option. Please run ./connect_device.sh -h for usage."