4 if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
5 echo "Device registration utility for Sentinel 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
29 if [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then
30 echo "Empty or incomplete parameters! Please run ./register_device.sh -h for usage."
39 echo "$MAC $IP $KEY $5" >> ~/sentinel_setup/register/devices.dat
41 # Insert into /etc/config/hostapd-psk
42 echo "$MAC $KEY" >> /etc/config/hostapd-psk
44 # Insert into /etc/config/dhcp
45 echo "" >> /etc/config/dhcp
47 if [ "$5" != "" ]; then # If device-name is not empty
48 echo "# $5" >> /etc/config/dhcp
51 echo "config host" >> /etc/config/dhcp
52 echo " option ip '$IP'" >> /etc/config/dhcp
53 echo " option mac '$MAC'" >> /etc/config/dhcp
55 if [ "$5" != "" ]; then # If device-name is not empty
56 echo " option name '$5'" >> /etc/config/dhcp
62 # Print list of devices
63 elif [ "$1" == "-l" ]; then
64 echo "List of devices"
65 cat ~/sentinel_setup/register/devices.dat
67 echo "/etc/config/hostapd-psk"
68 cat /etc/config/hostapd-psk
70 # Print only the devices' names list
71 elif [ "$1" == "-ln" ]; then
72 cat ~/sentinel_setup/register/devices.dat | awk '{print $4}'
74 # Delete device by MAC address
75 elif [ "$1" == "-dm" ]; then
76 # Make new file without the line containing specific MAC address then swap
77 sed -e "/$2/d" ~/sentinel_setup/register/devices.dat > tmp.dat
79 rm ~/sentinel_setup/register/devices.dat
80 mv tmp.dat ~/sentinel_setup/register/devices.dat
82 # update /etc/config/hostapd
83 sed -e "/$2/d" /etc/config/hostapd-psk > hostapd.tmp
84 rm /etc/config/hostapd-psk
85 mv hostapd.tmp /etc/config/hostapd-psk
87 # update /etc/config/dhcp
88 # get line number of dhcp including the MAC address
89 LN=$(sed -n "/$2/=" /etc/config/dhcp)
90 HEAD=$(expr ${LN} - 3)
92 # add 1, not 2, in case of no name line in target device
93 TAIL=$(expr ${LN} + 1)
94 sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp
96 mv dhcp.tmp /etc/config/dhcp
99 echo "device deleted!"
104 # Delete by name. Similar to deleting with MAC
105 elif [ "$1" == "-dn" ]; then
107 cp /etc/config/hostapd-psk /etc/config/hostapd-psk.bak
108 cp /etc/config/dhcp /etc/config/dhcp.bak
110 #Multiple name arguments can be given.
116 # Get MAC Address first looking up the devices.dat file
117 MAC=$(grep ${NAME} ~/sentinel_setup/register/devices.dat | awk '{print $1}')
119 # Make new file without the line containing specific device name then swap
120 sed -e "/${NAME}/d" ~/sentinel_setup/register/devices.dat > tmp.dat
122 rm ~/sentinel_setup/register/devices.dat
123 mv tmp.dat ~/sentinel_setup/register/devices.dat
125 # update /etc/config/hostapd
126 sed -e "/${MAC}/d" /etc/config/hostapd-psk > hostapd.tmp
127 rm /etc/config/hostapd-psk
128 mv hostapd.tmp /etc/config/hostapd-psk
130 # update /etc/config/dhcp
131 # get line number of dhcp including the MAC address
132 LN=$(sed -n "/${MAC}/=" /etc/config/dhcp)
133 HEAD=$(expr ${LN} - 3)
135 # add 1, not 2, in case of no name in the dhcp file
136 TAIL=$(expr ${LN} + 1)
137 sed "${HEAD},${TAIL}d" /etc/config/dhcp > dhcp.tmp
139 mv dhcp.tmp /etc/config/dhcp
143 echo "device deleted!"
149 echo "Unknown option. Please run ./register_device.sh -h for usage."