Renewing on router shell scripts for the phone app.
[lede.git] / vigilia_setup / register / version_3 / 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 "~/vigilia_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> <MAC address> <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 $5. If the argument is empty, let's use THE default one.
26         DEFAULT="1qaz2wsx3edc"
27         if [ ! -z "$5" ]; then
28                 DEFAULT=$5
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 ~/vigilia_setup/register/devices.dat
37         chmod 666 ~/vigilia_setup/register/devices.dat
38
39         # now ready to use devices.dat. Get numbers of records in devices.dat
40         NR=$(cat ~/vigilia_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
46         grep $3 leases.temp > temp.temp
47         rm leases.temp
48         mv temp.temp leases.temp        
49
50 #       for i in `seq 1 $NR`
51 #       do
52 #               #get line number(LN)
53 #               LN="${i}p"
54 #               #get target MAC address(TMAC) from devices.dat
55 #               TMAC=$(sed -n ${LN} ~/vigilia_setup/register/devices.dat | awk '{print $1}')
56 #               echo "TMAC: $TMAC"
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
59 #               rm leases.temp
60 #               mv leases.temp.temp leases.temp
61 #       done
62
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"
67
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}')
73         NAME=$4
74
75         # 2
76         # Add record to the database with ~/vigilia_setup/register/register_device.sh
77         echo "MAC: $MAC"
78         echo "IP: $IP"
79         echo "PW: $PW"
80         echo "Name: $NAME"
81         ~/vigilia_setup/register/register_device.sh -a $MAC $IP $PW $NAME
82
83         # 3
84         # rewind the default password and apply config files
85         uci commit
86         ~/vigilia_setup/register/change_default_pw.sh -ch $DEFAULT
87         /sbin/wifi
88
89 else
90         echo "Unknown option. Please run ./connect_device.sh -h for usage."
91
92 fi
93
94