Renewing on router shell scripts for the phone app.
[lede.git] / vigilia_setup / register / version_3 / register_device.sh
1 #!/bin/sh
2
3 # Print usage
4 if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
5         echo "Device registration utility for Vigilia system"
6         echo "This is a simple script that register a new device"
7         echo "into /etc/config/dhcp and /etc/config/hostapd-psk"
8         echo "Copyright (c) 2015-2017, Rahmadi Trimananda <rtrimana@uci.edu> PLRG@UCIrvine"
9         echo ""
10         echo "Usage:"
11         echo "  ./register_device.sh [-h]"
12         echo "  ./register_device.sh [-a <mac-address> <ip-address> <key> <device-name>]"
13         echo "  ./register_device.sh [-l]"
14         echo "  ./register_device.sh [-ln]"
15         echo "  ./register_device.sh [-dm <mac-address>]"
16         echo "  ./register_device.sh [-dn <device-name>]"
17         echo ""
18         echo "Options:"
19         echo "  -h      show this usage"
20         echo "  -a      adding device by putting MAC address, desired IP address, key, and device name (optional)"
21         echo "  -l      show list of devices registered"
22         echo "  -ln     show list of names of devices registered"
23         echo "  -dm     delete a specific registered device with MAC address"
24         echo "  -dn     delete a specific registered device with name"
25         echo ""
26
27 # add a device
28 elif [ "$1" == "-a" ]; then
29         while read line; do
30                 for word in $line; do
31                         if [ "$2" == $word ]; then
32                                 echo "MAC address: $2 is already used! Please use a different MAC address."
33                                 exit
34                         fi
35                         if [ "$3" == $word ]; then
36                                 echo "IP address: $3 is already used! Please use a different IP address."
37                                 exit
38                         fi
39                         if [ "$5" == $word ]; then
40                                 echo "Device name: $5 is already used! Please use a different Name."
41                                 exit
42                         fi
43                 done
44         done <devices.dat
45         if [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then
46                 echo "Empty or incomplete parameters! Please run ./register_device.sh -h for usage."
47
48         else
49                 # Add a new device
50                 MAC=$2
51                 IP=$3
52                 KEY=$4
53
54                 # Keep a local log
55                 echo "$MAC      $IP     $KEY    $5      $6" >> ~/vigilia_setup/register/devices.dat
56
57                 # Insert into /etc/config/hostapd-psk
58                 echo "$MAC $KEY" >> /etc/config/hostapd-psk
59
60                 # Insert into /etc/config/dhcp
61                 echo "" >> /etc/config/dhcp                                                                                                           
62                 if [ "$5" != "" ]; then # If device-name is not empty                                             
63                         echo "# $5" >> /etc/config/dhcp                                                           
64                 fi                                                                                                
65                                                                                                                   
66                 echo "config host" >> /etc/config/dhcp                                                            
67                 echo "  option ip '$IP'" >> /etc/config/dhcp                                                      
68                 echo "  option mac '$MAC'" >> /etc/config/dhcp                                                    
69                                                                                                                   
70                 if [ "$5" != "" ]; then # If device-name is not empty                                             
71                         echo "  option name '$5'" >> /etc/config/dhcp                                             
72                 fi                                                                                                
73                                                                                                                   
74     echo "Device added!"                                                                                          
75         fi                                                                                                        
76                                                                                                                   
77 # Print list of devices                                                                                           
78 elif [ "$1" == "-l" ]; then                                                                                
79         echo "List of devices"                                                                             
80         cat ~/vigilia_setup/register/devices.dat                                                          
81         echo ""                                                                                            
82         echo "/etc/config/hostapd-psk"                                                                     
83         cat /etc/config/hostapd-psk                                                                        
84                                                                                                            
85 # Print only the devices' names list                                                                       
86 elif [ "$1" == "-ln" ]; then                                                                               
87 #       cat ~/vigilia_setup/register/devices.dat | awk '{print $4}'                                       
88         cat ~/vigilia_setup/register/devices.dat | awk '{print $4," ",$1," ",$2}'                         
89                                                                                                            
90 # Delete device by MAC address                                                                           
91 elif [ "$1" == "-dm" ]; then                                                                             
92         # Make new file without the line containing specific MAC address then swap                       
93                                                                                                          
94         MAC=$2                                                                                      
95         if grep -q $MAC devices.dat; then                                                           
96                 echo "MAC Address found!"                                                           
97         else                                                                                        
98                 echo "MAC Address was not found. Please enter a valid MAC Address."                 
99         fi                                                                                          
100                                                                                                     
101         sed -e "/$2/d" ~/vigilia_setup/register/devices.dat > tmp.dat                              
102         chmod 666 tmp.dat                                                                           
103         rm ~/vigilia_setup/register/devices.dat                                            
104         mv tmp.dat ~/vigilia_setup/register/devices.dat                                    
105                                                                                             
106         # update /etc/config/hostapd                                                        
107         sed -e "/$2/d" /etc/config/hostapd-psk > hostapd.tmp                                
108         rm /etc/config/hostapd-psk                                                          
109         mv hostapd.tmp /etc/config/hostapd-psk                                              
110                                                                                             
111         # update /etc/config/dhcp                                                           
112         # get line number of dhcp including the MAC address                        
113         LN=$(sed -n "/$2/=" /etc/config/dhcp)                                      
114         HEAD=$(expr ${LN} - 3)                                                     
115                                                                                    
116         # add 1, not 2, in case of no name line in target device                   
117         TAIL=$(expr ${LN} + 1)                                                     
118         sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp     
119         rm /etc/config/dhcp                                                                                
120         mv dhcp.tmp /etc/config/dhcp                                                     
121                                                                                                                   
122         #show on screen                                                                  
123         echo "device deleted!"                                                                           
124                                                                                   
125         #apply change                                                              
126         /sbin/wifi                                                                
127                                                                                                                   
128 # Delete by name. Similar to deleting with MAC                                                                    
129 elif [ "$1" == "-dn" ]; then                                          
130         # back up first                                                                                           
131         cp /etc/config/hostapd-psk /etc/config/hostapd-psk.bak                    
132         cp /etc/config/dhcp /etc/config/dhcp.bak                                   
133                                                                                                            
134                                                                                   
135         #Multiple name arguments can be given.                               
136         VAR1=$1                                                                   
137         NAME=$2                                                                                          
138         FLAG=0                                                                    
139         shift 1                                                                                            
140         for arg in "$@"; do                                                                                                                                                                     
141                 NAME=${arg}                                                                         
142                 # Get MAC Address first looking up the devices.dat file      
143                 MAC=$(grep ${NAME} ~/vigilia_setup/register/devices.dat | awk '{print $1}')        
144                                                                                           
145                                                                                    
146                 # Make new file without the line containing specific device name then swap          
147                 sed -e "/${NAME}/d" ~/vigilia_setup/register/devices.dat > tmp.dat                 
148                 chmod 666 tmp.dat                                                           
149                 rm ~/vigilia_setup/register/devices.dat                                            
150                 mv tmp.dat ~/vigilia_setup/register/devices.dat                            
151                                                                                           
152                 # update /etc/config/hostapd                                       
153                 sed -e "/${MAC}/d" /etc/config/hostapd-psk > hostapd.tmp                  
154                 rm /etc/config/hostapd-psk                                         
155                 mv hostapd.tmp /etc/config/hostapd-psk                                      
156                                                                                             
157                 # update /etc/config/dhcp                                                   
158                 # get line number of dhcp including the MAC address                         
159                 LN=$(sed -n "/${MAC}/=" /etc/config/dhcp)                          
160                 HEAD=$(expr ${LN} - 3)                                                    
161                 #echo "ln: $LN"                                                    
162                 #echo "head: $HEAD"                                                         
163                                                                              
164                 # add 1, not 2, in case of no name in the dhcp file                         
165                                                                                                            
166                 TAIL=$(expr ${LN} + 1)                                                    
167                 #echo "Tail: $TAIL"                                                
168                 sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp                                  
169                 rm /etc/config/dhcp                                                                 
170                 mv dhcp.tmp /etc/config/dhcp                                                
171                                                                                                     
172                 #show on screen                                                             
173                 echo "device deleted!"                                                    
174         done                                                                       
175                                                                                           
176                                                                                    
177         #apply change                                                                       
178         /sbin/wifi                                                                          
179                                                                                             
180                                                                                             
181 else                                                                               
182         echo "Unknown option. Please run ./register_device.sh -h for usage."              
183                                                                                    
184 fi                                                                                          
185                                                                              
186                                             
187