-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.sh
78 lines (70 loc) · 2.23 KB
/
gateway.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# (C) Ron K. Irvine, 2020. All rights reserved.
# gateway.sh
# Script to monitor the Raspberry Pi's WiFi Internet gateway.
# Red LED:
# Off - trurned off
# Green LED:
# Off - No Internet Gateway
# On - Gateway detected, daytime hours
# Blink - Gateway detected, night hours
#
# Options
offAtNight=1
# daytime - daytime hours; 07:30 to 23:00
# don't foget the leading 0 on the hour "hh:mm", 5 characters
# note sting compare is limited to EQ, GT and LT
# so LE (less than or equal) is the same as NOT GT (not greater than)
isDaylight() {
currenttime=$(date +%H:%M)
if [[ ! "$currenttime" < "07:30" ]] && [[ "$currenttime" < "23:00" ]]; then
daytime=1
else
daytime=0
fi
printf "$currenttime: Daytime = $daytime\n"
}
# use the 'ip route' command to get the gateway's IP address
# an empty string if no gateway
gateway=$(ip route | grep "default via" | cut -d ' ' -f 3)
device=$(ip route | grep "default via" | cut -d ' ' -f 5)
if [ -z "$gateway" ]; then
printf "Gateway: Not Found...\n"
else
printf "Ping Gateway: $gateway - $device\n"
ping -q -w 1 -c 1 $gateway >/dev/null 2>/dev/null && echo Gateway - Ok! || echo Gateway - ERROR!
fi
# led0: Green Led - default usage is flash access indicator [mmc0]
led0=/sys/class/leds/led0
# disable [mmc0] trigger (control) for Green LED
sudo bash -c "echo none >$led0/trigger"
#led1: Red LED - default power on/off indicator
led1=/sys/class/leds/led1
# disable Red power LED - 0 = Off
sudo bash -c "echo 0 >$led1/brightness"
printf "Loop forever ... ^C to exit\n"
while [ 1 ]; do
isDaylight
gateway=$(ip route | grep "default via" | cut -d ' ' -f 3)
if [ -z "$gateway" ]; then
printf "Gateway - Down!\n"
# Green LED - 0 = Off
sudo bash -c "echo 0 >$led0/brightness"
else
printf "Gateway $gateway - Ok!\n"
# Green LED - 1 = On
if [ "$offAtNight" -eq "1" ]; then
# GREEN LED - status during the daytime, off at night
sudo bash -c "echo $daytime >$led0/brightness"
elif [ "$daytime" -eq "1" ]; then
# daytime - GREEN LED - On
sudo bash -c "echo 1 >$led0/brightness"
else
# at night - just a short blink of the GREEN LED
sudo bash -c "echo 1 >$led0/brightness"
sleep 0.001
sudo bash -c "echo 0 >$led0/brightness"
fi
fi
sleep 3
done