Renewing on router shell scripts for the phone app.
authorrtrimana <rtrimana@uci.edu>
Wed, 25 Jul 2018 00:08:53 +0000 (17:08 -0700)
committerrtrimana <rtrimana@uci.edu>
Wed, 25 Jul 2018 00:08:53 +0000 (17:08 -0700)
README
vigilia_setup/register/version_3/change_default_pw.sh [new file with mode: 0644]
vigilia_setup/register/version_3/connect_device.sh [new file with mode: 0644]
vigilia_setup/register/version_3/register_device.sh [new file with mode: 0644]

diff --git a/README b/README
index af16ae4b8396c328eddb0bfa9550b402a55801bf..42d8b6569b7b7ca7d1a507b278211628014aa902 100644 (file)
--- a/README
+++ b/README
@@ -82,7 +82,7 @@ default firewall setup from LEDE, we can do
 UCI when it is initializing the firewall rules when the system is booting up.
 
 10) We need to also create /root/vigilia_setup/register and copy the scripts in
-vigilia_setup/register/version_2 into it. These Shell scripts work with the
+vigilia_setup/register/version_3 into it. These Shell scripts work with the
 Android app that registers and deletes devices to and from the router.
 
 11) Last, we need to copy the setup scripts in vigilia_setup/setup into /setup on
diff --git a/vigilia_setup/register/version_3/change_default_pw.sh b/vigilia_setup/register/version_3/change_default_pw.sh
new file mode 100644 (file)
index 0000000..67c1c61
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+# Print usage
+if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
+       echo "This is a simple script that change the pre-shared key of a router"
+       echo "using UCI command change password written in /etc/config/wireless"
+       echo ""
+       echo "Usage:"
+       echo "  ./change_default_pw.sh [-h]"
+       echo "  ./change_default_pw.sh [-ch <specific_password>]"
+       echo ""
+       echo "Options:"
+       echo "  -h      show this usage"
+       echo "  -ch     change default into a specific password"
+       echo ""
+
+elif [ "$1" == "-ch" ]; then
+       # Change the wireless.key option in the config file.
+       PW=$2
+       uci set wireless.default_radio0.key=$PW
+       uci set wireless.default_radio1.key=$PW
+       uci commit
+       /sbin/wifi
+
+else
+       echo "Unknown option. Please run ./change_default_pw.sh -h for usage."
+fi
+
+
diff --git a/vigilia_setup/register/version_3/connect_device.sh b/vigilia_setup/register/version_3/connect_device.sh
new file mode 100644 (file)
index 0000000..d63635b
--- /dev/null
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+# Print usage
+if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
+       echo "This is a simple script that register connected device into the system"
+       echo "/etc/config/hostapd-psk /etc/config/dhcp will be changed accordingly"
+       echo "~/vigilia_setup/register/register_device.sh will be executed"
+       echo ""
+       echo "Usage:"
+       echo "  ./connect_device.sh [-h]"
+       echo "  ./connect_device.sh [-co <specific-pw> <MAC address> <name> <default_pw>]"
+       echo ""
+       echo "Options:"
+       echo "  -h      show this usage"
+       echo "  -co     connect a new device"
+       echo ""
+
+elif [ "$1" == "-co" ]; then
+
+       # 0
+       # Get password from $2
+       # Supposing that key for radio0 and radio1 are equal
+       PW=$2
+
+       # Save default password from $5. If the argument is empty, let's use THE default one.
+       DEFAULT="1qaz2wsx3edc"
+       if [ ! -z "$5" ]; then
+               DEFAULT=$5
+       fi
+
+       # 1
+       # Get MAC address and IP address from dhcp.leases file.
+       # Below scripts will find the most recently connected device by sorting the first column of lease file, 
+       # which is time of lease expiry, in epoch time
+       # Before that, the file named devices.dat could not exist, so touch it first
+       touch ~/vigilia_setup/register/devices.dat
+       chmod 666 ~/vigilia_setup/register/devices.dat
+
+       # now ready to use devices.dat. Get numbers of records in devices.dat
+       NR=$(cat ~/vigilia_setup/register/devices.dat | wc -l)
+
+       # use temp file for procedure so that we don't touch original lease file
+       cp /tmp/dhcp.leases leases.temp
+
+
+       grep $3 leases.temp > temp.temp
+       rm leases.temp
+       mv temp.temp leases.temp        
+
+#      for i in `seq 1 $NR`
+#      do
+#              #get line number(LN)
+#              LN="${i}p"
+#              #get target MAC address(TMAC) from devices.dat
+#              TMAC=$(sed -n ${LN} ~/vigilia_setup/register/devices.dat | awk '{print $1}')
+#              echo "TMAC: $TMAC"
+#              #remove the record with certain TMAC in the dhcp file so that we can get new one at the end
+#              sed -e /${TMAC}/d leases.temp > leases.temp.temp
+#              rm leases.temp
+#              mv leases.temp.temp leases.temp
+#      done
+
+       # Filter done. There should be only one line in leases.temp, but just in
+       # case sort the epoch time
+#      RECENT=$(sort -nrk1,1 leases.temp | head -1)
+#      echo "Recent: $RECENT"
+
+       # Get MAC, IP, NAME from RECENT
+       MAC=$(awk '{print $2}' leases.temp)
+       IP=$(awk '{print $3}' leases.temp)
+       # below was used when there were no name argument in the script
+       # NAME=$(echo $RECENT | awk '{print $4}')
+       NAME=$4
+
+       # 2
+       # Add record to the database with ~/vigilia_setup/register/register_device.sh
+       echo "MAC: $MAC"
+       echo "IP: $IP"
+       echo "PW: $PW"
+       echo "Name: $NAME"
+       ~/vigilia_setup/register/register_device.sh -a $MAC $IP $PW $NAME
+
+       # 3
+       # rewind the default password and apply config files
+       uci commit
+       ~/vigilia_setup/register/change_default_pw.sh -ch $DEFAULT
+       /sbin/wifi
+
+else
+       echo "Unknown option. Please run ./connect_device.sh -h for usage."
+
+fi
+
+
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                                                                                          
+                                                                             
+                                            
+