X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=vigilia_setup%2Fregister%2Fversion_2%2Fregister_device.sh;fp=vigilia_setup%2Fregister%2Fversion_2%2Fregister_device.sh;h=e9c28c1be663b258fd3039194b32294574ac2411;hb=7ff6b5c63dab61f55310ca9f99a079c5b0627be3;hp=0000000000000000000000000000000000000000;hpb=7de6013839a457b5735d7961b994cce0976747f3;p=lede.git diff --git a/vigilia_setup/register/version_2/register_device.sh b/vigilia_setup/register/version_2/register_device.sh new file mode 100755 index 0000000000..e9c28c1be6 --- /dev/null +++ b/vigilia_setup/register/version_2/register_device.sh @@ -0,0 +1,151 @@ +#!/bin/sh + +# Print usage +if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then + echo "Device registration utility for Sentinel 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 PLRG@UCIrvine" + echo "" + echo "Usage:" + echo " ./register_device.sh [-h]" + echo " ./register_device.sh [-a ]" + echo " ./register_device.sh [-l]" + echo " ./register_device.sh [-ln]" + echo " ./register_device.sh [-dm ]" + echo " ./register_device.sh [-dn ]" + 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 + 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" >> ~/sentinel_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 ~/sentinel_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 ~/sentinel_setup/register/devices.dat | awk '{print $4}' + +# Delete device by MAC address +elif [ "$1" == "-dm" ]; then + # Make new file without the line containing specific MAC address then swap + sed -e "/$2/d" ~/sentinel_setup/register/devices.dat > tmp.dat + chmod 666 tmp.dat + rm ~/sentinel_setup/register/devices.dat + mv tmp.dat ~/sentinel_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 + shift 1 + for arg in "$@" + do + NAME=${arg} + # Get MAC Address first looking up the devices.dat file + MAC=$(grep ${NAME} ~/sentinel_setup/register/devices.dat | awk '{print $1}') + + # Make new file without the line containing specific device name then swap + sed -e "/${NAME}/d" ~/sentinel_setup/register/devices.dat > tmp.dat + chmod 666 tmp.dat + rm ~/sentinel_setup/register/devices.dat + mv tmp.dat ~/sentinel_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) + + # add 1, not 2, in case of no name in the dhcp file + TAIL=$(expr ${LN} + 1) + sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp + rm /etc/config/dhcp + mv dhcp.tmp /etc/config/dhcp + done + + #show on screen + echo "device deleted!" + + #apply change + /sbin/wifi + +else + echo "Unknown option. Please run ./register_device.sh -h for usage." + +fi