#!/usr/local/bin/bash
# ===============================
# Variables
# ===============================
WAN_INTERFACE=WAN_DHCP
LOGFILE=/var/log/check_freebox.log
FREEBOX_URL="http://[2a01:xxxx:xxx:xxx::1]:1234"
MY_APP_ID="ipv4Checker.app"
MY_APP_TOKEN="9DKdJddd75ee7743jdjdjd/89DNHED73JD38D3JIDndudYuD"
TELEGRAM_DEST="1234567890"
TELEGRAM_TOKEN="037448484:XE7DJDdd45d7DDe:D8DUdkddUD"
HEALTHCHECKS="D838383-DJDJDJD-D6D6D6-DHDHDH"
# ===============================
# Telegram Notification
# ===============================
notification() {
/usr/local/bin/curl -s --data chat_id=$TELEGRAM_DEST \
--data-urlencode text="$@" \
"https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" > /dev/null
}
# ===============================
# WAN status
# ===============================
wan_status() {
STATUS=$(/usr/local/sbin/pfSsh.php playback gatewaystatus brief | awk '$1 == "WAN_DHCP" { print $NF }')
case $STATUS in
none)
return 0
;;
down)
return 1
;;
esac
}
# ===============================
# Main loop
# ===============================
curl --silent --output /dev/null "https://hc-ping.com/$HEALTHCHECKS"
wan_status
case $? in
0)
echo -en "WAN up : $(date)\n" >> $LOGFILE
;;
1)
notification "WAN down"
echo -en "WAN down : $(date)\n" >> $LOGFILE
source /root/freeboxos_bash_api.sh
login_freebox "$MY_APP_ID" "$MY_APP_TOKEN"
# Router mode
answer=$(call_freebox_api_put 'lan/config/' '{"mode":"router","ip":"192.168.1.1","name":"Freebox Server","name_dns":"freebox-server","name_mdns":"Freebox-Server","name_netbios":"Freebox_Server"}') || return 1
_check_success "$answer" || return 1
mode=$(get_json_value_for_key "$answer" result.mode)
notification "Mode $mode activé"
echo -en "Mode $mode activé : $(date)\n" >> $LOGFILE
sleep 1
# Bridge mode
answer=$(call_freebox_api_put 'lan/config/' '{"mode":"bridge","ip":"192.168.1.1","name":"Freebox Server","name_dns":"freebox-server","name_mdns":"Freebox-Server","name_netbios":"Freebox_Server"}') || return 1
_check_success "$answer" || return 1
mode=$(get_json_value_for_key "$answer" result.mode)
notification "Mode $mode activé"
echo -en "Mode $mode activé : $(date)\n" >> $LOGFILE
sleep 60
wan_status && (notification "WAN back up"; echo -en "WAN back up : $(date)\n" >> $LOGFILE)
;;
esac