-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsms_on_boot_combined.sh
More file actions
132 lines (115 loc) · 3.08 KB
/
sms_on_boot_combined.sh
File metadata and controls
132 lines (115 loc) · 3.08 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/sh
LOG="/tmp/sms_on_boot_combined.log"
STATE="/etc/sms_on_boot_combined.last"
COOLDOWN=300
# Defaults (can be overridden by /etc/sms_on_boot_combined.conf)
PHONE="+11234567890"
PREFER="textbelt"
TEXTBELT_KEY=""
CONF="/etc/sms_on_boot_combined.conf"
if [ -f "$CONF" ]; then
# shellcheck disable=SC1090
. "$CONF"
fi
log() { echo "[$(date '+%F %T')] $*" >> "$LOG"; }
log "Script start"
find_wan_iface() {
dev="$(ip route 2>/dev/null | awk '/^default /{for(i=1;i<=NF;i++) if($i=="dev"){print $(i+1); exit}}')"
if [ -n "$dev" ]; then
case "$dev" in
lo|br-lan|lan*|eth*|en*|wl*|wlan*|phy*|ap*|bat*|ifb*|docker*|veth*|tun*|wg*|tailscale* ) ;;
*) echo "$dev"; return 0 ;;
esac
fi
for dev in $(ls /sys/class/net 2>/dev/null); do
case "$dev" in
wan*|ppp*|wwan*|usb*|rmnet*|mhi*|cdc*|en*|eth* )
ip -4 addr show dev "$dev" 2>/dev/null | grep -q "inet " && { echo "$dev"; return 0; }
;;
esac
done
return 1
}
send_textbelt() {
if [ -z "$TEXTBELT_KEY" ]; then
log "Textbelt: key not set"
return 1
fi
if ! command -v curl >/dev/null 2>&1; then
log "Textbelt: curl not found"
return 1
fi
RESP="$(curl -sS --connect-timeout 5 --max-time 15 -X POST https://textbelt.com/text \
--data-urlencode phone="$PHONE" \
--data-urlencode message="$MSG" \
-d key="$TEXTBELT_KEY" 2>>"$LOG" || true)"
echo "[$(date '+%F %T')] Textbelt response: $RESP" >> "$LOG"
echo "$RESP" | grep -q '"success":[[:space:]]*true'
}
send_sendsms() {
if ! command -v sendsms >/dev/null 2>&1; then
log "sendsms: sendsms not found"
return 1
fi
sendsms "$PHONE" "$MSG" international >/dev/null 2>&1
}
# Cooldown guard
now="$(date +%s)"
if [ -f "$STATE" ]; then
last="$(cat "$STATE" 2>/dev/null | tr -dc '0-9')"
if [ -n "$last" ] && [ $((now-last)) -lt "$COOLDOWN" ]; then
log "Cooldown active, skipping"
exit 0
fi
fi
# Only wait for SMS daemons if sendsms exists
if command -v sendsms >/dev/null 2>&1; then
i=0
while [ $i -lt 60 ]; do
pidof sms_manager >/dev/null 2>&1 && pidof smsd >/dev/null 2>&1 && break
sleep 2
i=$((i+1))
done
fi
WAN_IFACE="$(find_wan_iface 2>/dev/null || true)"
WAN_IP=""
if [ -n "$WAN_IFACE" ]; then
WAN_IP="$(ip -4 addr show dev "$WAN_IFACE" 2>/dev/null | awk '/inet /{print $2}' | cut -d/ -f1 | head -n1)"
fi
MSG="GL.iNet router booted after power outage.
Time: $(date '+%F %T %Z')"
[ -n "$WAN_IFACE" ] && MSG="$MSG
WAN IF: $WAN_IFACE"
[ -n "$WAN_IP" ] && MSG="$MSG
WAN IP: $WAN_IP"
# If no WAN IP, skip Textbelt
if [ -z "$WAN_IP" ]; then
log "No WAN IP detected; skipping Textbelt"
TEXTBELT_KEY=""
fi
log "Preferred provider: $PREFER"
if [ "$PREFER" = "textbelt" ]; then
if send_textbelt; then
log "Sent via Textbelt"
echo "$now" > "$STATE"
exit 0
fi
if send_sendsms; then
log "Sent via sendsms"
echo "$now" > "$STATE"
exit 0
fi
else
if send_sendsms; then
log "Sent via sendsms"
echo "$now" > "$STATE"
exit 0
fi
if send_textbelt; then
log "Sent via Textbelt"
echo "$now" > "$STATE"
exit 0
fi
fi
log "All providers failed"
exit 1