Completing ZigbeeTest with doorlock test
[iot2.git] / benchmarks / other / OpenWrt / 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/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 ""
15         echo "Options:"
16         echo "  -h      show this usage"
17         echo "  -a      adding device by putting MAC address, desired IP address, key, and device name (optional)"
18         echo "  -l      show list of devices registered"
19         echo ""
20
21 elif [ "$1" == "-a" ]; then
22
23         if [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then
24                 echo "Empty or incomplete parameters! Please run ./register_device.sh -h for usage."
25         else
26                 # Add a new device
27                 MAC=$2
28                 IP=$3
29                 KEY=$4
30
31                 # Keep a local log
32                 echo "$MAC      $IP     $KEY    $5" >> devices.dat
33
34                 # Insert into /etc/hostapd-psk
35                 echo "$MAC $KEY" >> /etc/hostapd-psk
36
37                 # Insert into /etc/config/dhcp
38                 echo "" >> /etc/config/dhcp
39                 if [ "$5" != "" ]; then # If device-name is not empty
40                         echo "# $5" >> /etc/config/dhcp
41                 fi
42                 echo "config host" >> /etc/config/dhcp
43                 echo "  option ip '$IP'" >> /etc/config/dhcp
44                 echo "  option mac '$MAC'" >> /etc/config/dhcp
45
46                 if [ "$5" != "" ]; then # If device-name is not empty
47                         echo "  option name '$5'" >> /etc/config/dhcp
48                 fi
49                 echo "Device added!"
50         fi
51                                                  
52 elif [ "$1" == "-l" ]; then
53         # Print list of devices
54         echo "List of devices"
55         cat devices.dat
56         echo ""
57         echo "/etc/hostapd-psk"
58         cat /etc/hostapd-psk
59 else
60         echo "Unknown option. Please run ./register_device.sh -h for usage."
61 fi
62