Adjusting setup files to the most up-to-date.
[lede.git] / sentinel_setup / register / version_2 / register_device.sh
1 #!/bin/sh
2
3 # Print usage
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"
9         echo ""
10         echo "Usage:"
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>]"
17         echo ""
18         echo "Options:"
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"
25         echo ""
26
27 # add a device
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."
31
32         else
33                 # Add a new device
34                 MAC=$2
35                 IP=$3
36                 KEY=$4
37
38                 # Keep a local log
39                 echo "$MAC      $IP     $KEY    $5" >> ~/sentinel_setup/register/devices.dat
40
41                 # Insert into /etc/config/hostapd-psk
42                 echo "$MAC $KEY" >> /etc/config/hostapd-psk
43
44                 # Insert into /etc/config/dhcp
45                 echo "" >> /etc/config/dhcp
46
47                 if [ "$5" != "" ]; then # If device-name is not empty
48                         echo "# $5" >> /etc/config/dhcp
49                 fi
50
51                 echo "config host" >> /etc/config/dhcp
52                 echo "  option ip '$IP'" >> /etc/config/dhcp
53                 echo "  option mac '$MAC'" >> /etc/config/dhcp
54
55                 if [ "$5" != "" ]; then # If device-name is not empty
56                         echo "  option name '$5'" >> /etc/config/dhcp
57                 fi
58
59     echo "Device added!"
60         fi
61
62 # Print list of devices
63 elif [ "$1" == "-l" ]; then
64         echo "List of devices"
65         cat ~/sentinel_setup/register/devices.dat
66         echo ""
67         echo "/etc/config/hostapd-psk"
68         cat /etc/config/hostapd-psk
69
70 # Print only the devices' names list 
71 elif [ "$1" == "-ln" ]; then
72         cat ~/sentinel_setup/register/devices.dat | awk '{print $4}'
73
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
78         chmod 666 tmp.dat
79         rm ~/sentinel_setup/register/devices.dat
80         mv tmp.dat ~/sentinel_setup/register/devices.dat
81
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
86
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)
91
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
95         rm /etc/config/dhcp
96         mv dhcp.tmp /etc/config/dhcp
97
98         #show on screen
99         echo "device deleted!"
100
101         #apply change
102         /sbin/wifi
103
104 # Delete by name. Similar to deleting with MAC
105 elif [ "$1" == "-dn" ]; then
106         # back up first
107         cp /etc/config/hostapd-psk /etc/config/hostapd-psk.bak
108         cp /etc/config/dhcp /etc/config/dhcp.bak
109
110         #Multiple name arguments can be given.
111         VAR1=$1
112         shift 1
113         for arg in "$@"
114         do
115                 NAME=${arg}
116                 # Get MAC Address first looking up the devices.dat file
117                 MAC=$(grep ${NAME} ~/sentinel_setup/register/devices.dat | awk '{print $1}')
118
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
121                 chmod 666 tmp.dat
122                 rm ~/sentinel_setup/register/devices.dat
123                 mv tmp.dat ~/sentinel_setup/register/devices.dat
124
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
129
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)
134
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
138                 rm /etc/config/dhcp
139                 mv dhcp.tmp /etc/config/dhcp
140         done
141
142         #show on screen
143         echo "device deleted!"
144
145         #apply change
146         /sbin/wifi
147
148 else
149         echo "Unknown option. Please run ./register_device.sh -h for usage."
150
151 fi