Renaming Sentinel to Vigilia; adjusting scripts; adding brctl option in menuconfig...
[lede.git] / vigilia_setup / register / version_2 / connect_device.sh
1 #!/bin/sh
2
3 # Print usage
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"
8         echo ""
9         echo "Usage:"
10         echo "  ./connect_device.sh [-h]"
11         echo "  ./connect_device.sh [-co <specific-pw> <device_name> <default_pw>]"
12         echo ""
13         echo "Options:"
14         echo "  -h      show this usage"
15         echo "  -co     connect a new device"
16         echo ""
17
18 elif [ "$1" == "-co" ]; then
19
20         # 0
21         # Get password from $2
22         # Supposing that key for radio0 and radio1 are equal
23         PW=$2
24
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
28                 DEFAULT=$4
29         fi
30
31         # 1
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
38
39         # now ready to use devices.dat. Get numbers of records in devices.dat
40         NR=$(cat ~/sentinel_setup/register/devices.dat | wc -l)
41
42         # use temp file for procedure so that we don't touch original lease file
43         cp /tmp/dhcp.leases leases.temp
44
45         for i in `seq 1 $NR`
46         do
47                 #get line number(LN)
48                 LN="${i}p"
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
53                 rm leases.temp
54                 mv leases.temp.temp leases.temp
55         done
56
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)
60
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}')
66         NAME=$3
67
68         # 2
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
71
72         # 3
73         # rewind the default password and apply config files
74         uci commit
75         ~/sentinel_setup/register/change_default_pw.sh -ch $DEFAULT
76         /sbin/wifi
77
78 else
79         echo "Unknown option. Please run ./connect_device.sh -h for usage."
80
81 fi