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"
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>]"
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"
28 elif [ "$1" == "-a" ]; then
31 if [ "$2" == $word ]; then
32 echo "MAC address: $2 is already used! Please use a different MAC address."
35 if [ "$3" == $word ]; then
36 echo "IP address: $3 is already used! Please use a different IP address."
39 if [ "$5" == $word ]; then
40 echo "Device name: $5 is already used! Please use a different Name."
45 if [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then
46 echo "Empty or incomplete parameters! Please run ./register_device.sh -h for usage."
55 echo "$MAC $IP $KEY $5 $6" >> ~/vigilia_setup/register/devices.dat
57 # Insert into /etc/config/hostapd-psk
58 echo "$MAC $KEY" >> /etc/config/hostapd-psk
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
66 echo "config host" >> /etc/config/dhcp
67 echo " option ip '$IP'" >> /etc/config/dhcp
68 echo " option mac '$MAC'" >> /etc/config/dhcp
70 if [ "$5" != "" ]; then # If device-name is not empty
71 echo " option name '$5'" >> /etc/config/dhcp
77 # Print list of devices
78 elif [ "$1" == "-l" ]; then
79 echo "List of devices"
80 cat ~/vigilia_setup/register/devices.dat
82 echo "/etc/config/hostapd-psk"
83 cat /etc/config/hostapd-psk
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}'
90 # Delete device by MAC address
91 elif [ "$1" == "-dm" ]; then
92 # Make new file without the line containing specific MAC address then swap
95 if grep -q $MAC devices.dat; then
96 echo "MAC Address found!"
98 echo "MAC Address was not found. Please enter a valid MAC Address."
101 sed -e "/$2/d" ~/vigilia_setup/register/devices.dat > tmp.dat
103 rm ~/vigilia_setup/register/devices.dat
104 mv tmp.dat ~/vigilia_setup/register/devices.dat
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
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)
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
120 mv dhcp.tmp /etc/config/dhcp
123 echo "device deleted!"
128 # Delete by name. Similar to deleting with MAC
129 elif [ "$1" == "-dn" ]; then
131 cp /etc/config/hostapd-psk /etc/config/hostapd-psk.bak
132 cp /etc/config/dhcp /etc/config/dhcp.bak
135 #Multiple name arguments can be given.
142 # Get MAC Address first looking up the devices.dat file
143 MAC=$(grep ${NAME} ~/vigilia_setup/register/devices.dat | awk '{print $1}')
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
149 rm ~/vigilia_setup/register/devices.dat
150 mv tmp.dat ~/vigilia_setup/register/devices.dat
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
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)
164 # add 1, not 2, in case of no name in the dhcp file
166 TAIL=$(expr ${LN} + 1)
168 sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp
170 mv dhcp.tmp /etc/config/dhcp
173 echo "device deleted!"
182 echo "Unknown option. Please run ./register_device.sh -h for usage."