Renewing on router shell scripts for the phone app.
[lede.git] / vigilia_setup / register / version_3 / register_device.sh
diff --git a/vigilia_setup/register/version_3/register_device.sh b/vigilia_setup/register/version_3/register_device.sh
new file mode 100644 (file)
index 0000000..f31dc3a
--- /dev/null
@@ -0,0 +1,187 @@
+#!/bin/sh
+
+# Print usage
+if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
+        echo "Device registration utility for Vigilia system"
+        echo "This is a simple script that register a new device"
+        echo "into /etc/config/dhcp and /etc/config/hostapd-psk"
+        echo "Copyright (c) 2015-2017, Rahmadi Trimananda <rtrimana@uci.edu> PLRG@UCIrvine"
+        echo ""
+        echo "Usage:"
+        echo "  ./register_device.sh [-h]"
+        echo "  ./register_device.sh [-a <mac-address> <ip-address> <key> <device-name>]"
+        echo "  ./register_device.sh [-l]"
+        echo "  ./register_device.sh [-ln]"
+        echo "  ./register_device.sh [-dm <mac-address>]"
+        echo "  ./register_device.sh [-dn <device-name>]"
+        echo ""
+        echo "Options:"
+        echo "  -h      show this usage"
+        echo "  -a      adding device by putting MAC address, desired IP address, key, and device name (optional)"
+        echo "  -l      show list of devices registered"
+        echo "  -ln     show list of names of devices registered"
+        echo "  -dm     delete a specific registered device with MAC address"
+        echo "  -dn     delete a specific registered device with name"
+        echo ""
+
+# add a device
+elif [ "$1" == "-a" ]; then
+        while read line; do
+                for word in $line; do
+                        if [ "$2" == $word ]; then
+                                echo "MAC address: $2 is already used! Please use a different MAC address."
+                                exit
+                        fi
+                        if [ "$3" == $word ]; then
+                                echo "IP address: $3 is already used! Please use a different IP address."
+                                exit
+                        fi
+                        if [ "$5" == $word ]; then
+                                echo "Device name: $5 is already used! Please use a different Name."
+                                exit
+                        fi
+                done
+        done <devices.dat
+        if [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then
+                echo "Empty or incomplete parameters! Please run ./register_device.sh -h for usage."
+
+        else
+                # Add a new device
+                MAC=$2
+                IP=$3
+                KEY=$4
+
+                # Keep a local log
+                echo "$MAC      $IP     $KEY    $5     $6" >> ~/vigilia_setup/register/devices.dat
+
+                # Insert into /etc/config/hostapd-psk
+                echo "$MAC $KEY" >> /etc/config/hostapd-psk
+
+                # Insert into /etc/config/dhcp
+                echo "" >> /etc/config/dhcp                                                                                                           
+                if [ "$5" != "" ]; then # If device-name is not empty                                             
+                        echo "# $5" >> /etc/config/dhcp                                                           
+                fi                                                                                                
+                                                                                                                  
+                echo "config host" >> /etc/config/dhcp                                                            
+                echo "  option ip '$IP'" >> /etc/config/dhcp                                                      
+                echo "  option mac '$MAC'" >> /etc/config/dhcp                                                    
+                                                                                                                  
+                if [ "$5" != "" ]; then # If device-name is not empty                                             
+                        echo "  option name '$5'" >> /etc/config/dhcp                                             
+                fi                                                                                                
+                                                                                                                  
+    echo "Device added!"                                                                                          
+        fi                                                                                                        
+                                                                                                                  
+# Print list of devices                                                                                           
+elif [ "$1" == "-l" ]; then                                                                                
+        echo "List of devices"                                                                             
+        cat ~/vigilia_setup/register/devices.dat                                                          
+        echo ""                                                                                            
+        echo "/etc/config/hostapd-psk"                                                                     
+        cat /etc/config/hostapd-psk                                                                        
+                                                                                                           
+# Print only the devices' names list                                                                       
+elif [ "$1" == "-ln" ]; then                                                                               
+#       cat ~/vigilia_setup/register/devices.dat | awk '{print $4}'                                       
+        cat ~/vigilia_setup/register/devices.dat | awk '{print $4," ",$1," ",$2}'                         
+                                                                                                           
+# Delete device by MAC address                                                                           
+elif [ "$1" == "-dm" ]; then                                                                             
+        # Make new file without the line containing specific MAC address then swap                       
+                                                                                                         
+        MAC=$2                                                                                      
+        if grep -q $MAC devices.dat; then                                                           
+                echo "MAC Address found!"                                                           
+        else                                                                                        
+                echo "MAC Address was not found. Please enter a valid MAC Address."                 
+        fi                                                                                          
+                                                                                                    
+        sed -e "/$2/d" ~/vigilia_setup/register/devices.dat > tmp.dat                              
+        chmod 666 tmp.dat                                                                           
+        rm ~/vigilia_setup/register/devices.dat                                            
+        mv tmp.dat ~/vigilia_setup/register/devices.dat                                    
+                                                                                            
+        # update /etc/config/hostapd                                                        
+        sed -e "/$2/d" /etc/config/hostapd-psk > hostapd.tmp                                
+        rm /etc/config/hostapd-psk                                                          
+        mv hostapd.tmp /etc/config/hostapd-psk                                              
+                                                                                            
+        # update /etc/config/dhcp                                                           
+        # get line number of dhcp including the MAC address                        
+        LN=$(sed -n "/$2/=" /etc/config/dhcp)                                      
+        HEAD=$(expr ${LN} - 3)                                                     
+                                                                                   
+        # add 1, not 2, in case of no name line in target device                   
+        TAIL=$(expr ${LN} + 1)                                                     
+        sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp     
+        rm /etc/config/dhcp                                                                                
+        mv dhcp.tmp /etc/config/dhcp                                                     
+                                                                                                                  
+        #show on screen                                                                  
+        echo "device deleted!"                                                                           
+                                                                                  
+        #apply change                                                              
+        /sbin/wifi                                                                
+                                                                                                                  
+# Delete by name. Similar to deleting with MAC                                                                    
+elif [ "$1" == "-dn" ]; then                                          
+        # back up first                                                                                           
+        cp /etc/config/hostapd-psk /etc/config/hostapd-psk.bak                    
+        cp /etc/config/dhcp /etc/config/dhcp.bak                                   
+                                                                                                           
+                                                                                  
+        #Multiple name arguments can be given.                               
+        VAR1=$1                                                                   
+        NAME=$2                                                                                          
+        FLAG=0                                                                    
+        shift 1                                                                                            
+        for arg in "$@"; do                                                                                                                                                                     
+                NAME=${arg}                                                                         
+                # Get MAC Address first looking up the devices.dat file      
+                MAC=$(grep ${NAME} ~/vigilia_setup/register/devices.dat | awk '{print $1}')        
+                                                                                          
+                                                                                   
+                # Make new file without the line containing specific device name then swap          
+                sed -e "/${NAME}/d" ~/vigilia_setup/register/devices.dat > tmp.dat                 
+                chmod 666 tmp.dat                                                           
+                rm ~/vigilia_setup/register/devices.dat                                            
+                mv tmp.dat ~/vigilia_setup/register/devices.dat                            
+                                                                                          
+                # update /etc/config/hostapd                                       
+                sed -e "/${MAC}/d" /etc/config/hostapd-psk > hostapd.tmp                  
+                rm /etc/config/hostapd-psk                                         
+                mv hostapd.tmp /etc/config/hostapd-psk                                      
+                                                                                            
+                # update /etc/config/dhcp                                                   
+                # get line number of dhcp including the MAC address                         
+                LN=$(sed -n "/${MAC}/=" /etc/config/dhcp)                          
+                HEAD=$(expr ${LN} - 3)                                                    
+                #echo "ln: $LN"                                                    
+                #echo "head: $HEAD"                                                         
+                                                                             
+                # add 1, not 2, in case of no name in the dhcp file                         
+                                                                                                           
+                TAIL=$(expr ${LN} + 1)                                                    
+                #echo "Tail: $TAIL"                                                
+                sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp                                  
+                rm /etc/config/dhcp                                                                 
+                mv dhcp.tmp /etc/config/dhcp                                                
+                                                                                                    
+                #show on screen                                                             
+                echo "device deleted!"                                                    
+        done                                                                       
+                                                                                          
+                                                                                   
+        #apply change                                                                       
+        /sbin/wifi                                                                          
+                                                                                            
+                                                                                            
+else                                                                               
+        echo "Unknown option. Please run ./register_device.sh -h for usage."              
+                                                                                   
+fi                                                                                          
+                                                                             
+                                            
+