forked from zippyy/GL.iNet-CellularModels-SMSonBoot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsms_on_boot.sh
More file actions
119 lines (101 loc) · 3.2 KB
/
sms_on_boot.sh
File metadata and controls
119 lines (101 loc) · 3.2 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
#!/bin/sh
LOG="/tmp/sms_on_boot.log"
STATE="/etc/sms_on_boot.last"
COOLDOWN=300 # seconds (5 min)
PHONE="+13038675309" # <-- change if needed
WEBHOOK_URL="" # optional: POST JSON payload to this URL
WEBHOOK_CONNECT_TIMEOUT=5
WEBHOOK_TIMEOUT=15
log() { echo "[$(date '+%F %T')] $*" >> "$LOG"; }
json_escape() {
printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e ':a;N;$!ba;s/\n/\\n/g'
}
# Try to find the active WAN interface (cellular/wan) in a model-agnostic way.
# Priority:
# 1) default route device (common + accurate)
# 2) known cellular interface name patterns with an IPv4 address
find_active_wan_iface() {
# 1) Default route dev
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* ) ;;
* )
ip -4 addr show dev "$dev" 2>/dev/null | grep -q "inet " && { echo "$dev"; return 0; }
;;
esac
fi
# 2) Pattern scan: cellular interfaces often match these
for dev in $(ls /sys/class/net 2>/dev/null); do
case "$dev" in
rmnet*|wwan*|usb*|cdc*|mhi*|ccmni* )
ip -4 addr show dev "$dev" 2>/dev/null | grep -q "inet " && { echo "$dev"; return 0; }
;;
esac
done
return 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 ($((now-last))s < ${COOLDOWN}s), skipping."
exit 0
fi
fi
# Wait up to 2 minutes for SMS services + modem to be up
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
WAN_IFACE="$(find_active_wan_iface || 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
Cell IP: $WAN_IP"
send_webhook() {
[ -z "$WEBHOOK_URL" ] && return 0
if ! command -v curl >/dev/null 2>&1; then
log "Webhook: curl not found"
return 1
fi
payload="$(printf '{"event":"boot","time":"%s","wan_iface":"%s","wan_ip":"%s","message":"%s"}' \
"$(date '+%F %T %Z')" \
"$(json_escape "${WAN_IFACE:-}")" \
"$(json_escape "${WAN_IP:-}")" \
"$(json_escape "$MSG")")"
resp="$(curl -sS --connect-timeout "$WEBHOOK_CONNECT_TIMEOUT" --max-time "$WEBHOOK_TIMEOUT" \
-H "Content-Type: application/json" -d "$payload" -w '\n%{http_code}' \
"$WEBHOOK_URL" 2>>"$LOG" || true)"
body="$(printf '%s' "$resp" | sed '$d')"
code="$(printf '%s' "$resp" | tail -n 1)"
[ -n "$body" ] && log "Webhook response body: $body"
[ -n "$code" ] && log "Webhook response code: $code"
case "$code" in
2*|3*) return 0 ;;
esac
return 1
}
if send_webhook; then
log "Webhook sent."
else
log "Webhook failed."
fi
log "Sending SMS to $PHONE"
if sendsms "$PHONE" "$MSG" international; then
log "SMS sent."
echo "$now" > "$STATE"
exit 0
else
log "SMS failed."
exit 1
fi