Skip to content

Instantly share code, notes, and snippets.

@JayBrown
Last active March 1, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayBrown/269d118ee69ffff18e6657e939c194ec to your computer and use it in GitHub Desktop.
Save JayBrown/269d118ee69ffff18e6657e939c194ec to your computer and use it in GitHub Desktop.
Fritz!Reconnect (macOS) – tell Fritz!Box to reconnect to WAN to get a new public IP address (tested on model 7490)
#!/bin/zsh
# shellcheck shell=bash
# reconnect v0.2
# macOS / Fritz!Box (tested on model 7490)
export LANG=en_US.UTF-8
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
###
fritz_ip="192.168.178.1"
fritz_port="49000"
###
process="local.lcars.reconnect"
uiprocess="Fritz!Reconnect"
account=$(id -u)
logloc="/tmp/$process.log"
currentdate=$(date)
if ! [[ -f $logloc ]] ; then
echo "++++++++ $currentdate ++++++++" > "$logloc"
else
echo -e "\n++++++++ $currentdate ++++++++" >> "$logloc"
fi
exec > >(tee -a "$logloc") 2>&1
_currentip () {
curl "http://$fritz_ip:$fritz_port/igdupnp/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" -d "<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>" -s \
| grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>' 2>/dev/null
}
_currentip-fb () {
dig +short myip.opendns.com @resolver1.opendns.com 2>/dev/null
}
error=false
old_ip=$(_currentip)
if ! [[ $old_ip ]] ; then
old_ip=$(_currentip-fb)
if ! [[ $old_ip ]] ; then
echo "Error: no IP address"
osascript -e 'beep' -e 'delay 0.5' &>/dev/null &
osascript &>/dev/null << EOT
tell application "System Events"
display notification "❌ Error: no IP address" with title "$uiprocess [" & "$account" & "]" subtitle "Computer offline?"
end tell
EOT
exit 1
else
ipsource="OpenDNS"
fi
else
ipsource="FritzBox"
fi
echo "Current IP: $old_ip ($ipsource)"
if ! curl -s "http://$fritz_ip:$fritz_port/igdupnp/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination" -d "<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:ForceTermination xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>" &>/dev/null ; then
error=true
echo "Error: cURL"
osascript -e 'beep' -e 'delay 0.5' &>/dev/null &
osascript &>/dev/null << EOT
tell application "System Events"
display notification "❌ Error: cURL" with title "$uiprocess [" & "$account" & "]" subtitle "Reconnect command failed!"
end tell
EOT
else
ipsource="FritzBox"
count=0
while true
do
[[ $count -eq 3 ]] && break
sleep 3
new_ip=$(_currentip)
if [[ $new_ip != "$old_ip" ]] ; then
break
else
((count++))
fi
done
if [[ $count -eq 3 ]] ; then
sleep 3
new_ip=$(_currentip-fb)
ipsource="OpenDNS"
fi
if ! [[ $new_ip ]] ; then
echo "Error: no new IP address"
osascript -e 'beep' -e 'delay 0.5' &>/dev/null &
osascript &>/dev/null << EOT
tell application "System Events"
display notification "❌ Error: no new IP address" with title "$uiprocess [" & "$account" & "]" subtitle "Computer offline?"
end tell
EOT
exit 1
fi
echo "Current IP: $new_ip ($ipsource)"
if [[ $new_ip == "$old_ip" ]] ; then
error=true
echo "Error: IP unchanged"
osascript -e 'beep' -e 'delay 0.5' &>/dev/null &
osascript &>/dev/null << EOT
tell application "System Events"
display notification "⚠️ Public IP address unchanged!" with title "$uiprocess [" & "$account" & "]" subtitle "$old_ip"
end tell
EOT
else
echo "WAN reconnected with new IP address"
osascript &>/dev/null << EOT
tell application "System Events"
display notification "✅ WAN reconnected" with title "$uiprocess [" & "$account" & "]" subtitle "$old_ip > $new_ip"
end tell
EOT
fi
fi
$error && exit 1
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment