Fritz Box External IP

From JoBaPedia
Jump to navigation Jump to search

DynDNS

Recently dyndns updates on external IP changes did not always work. This should make it more robust.

IP from fritzbox taken from https://wiki.ubuntuusers.de/FritzBox/Skripte/

Read external IP from Fritzbox

/usr/local/bin/fb-ip.sh

#!/bin/bash

if [ "$1" == "" ]; then
  FB="fritz.box"
else
  FB="$1"
fi

curl "http://${FB}:49000/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}\>'

Compare external IP with DNS entry

/usr/local/bin/check-ip.sh

#!/bin/bash

if [ "$1" == "" ]; then
  CN="banzhaf.chickenkiller.com"
else
  CN="$1"
fi

if [ "$2" == "" ]; then
  FB="fritz.box"
else
  FB="$2"
fi

XIP=`/usr/local/bin/fb-ip.sh "$FB"`
DIP=`dig +short "$CN"`
if [ "$XIP" == "$DIP" ]; then
  echo "OK"
  exit 0
fi
echo "ERROR"
exit 1

Update DynDNS if external IP does not match DNS entry

if there is a mismatch between external IP and DNS, give it some time and then fix it

/usr/local/bin/ensure-ip.sh

#!/bin/bash

DELAY_S=120

if [ "$1" == "" ]; then
  CN="banzhaf.chickenkiller.com"
else
  CN="$1"
fi

if [ "$2" == "" ]; then
  FB="fritz.box"
else
  FB="$2"
fi

if ! check-ip.sh "$CN" "$FB" >/dev/null; then
  echo "Check IP failed. Waiting to settle"
  SINCE=$SECONDS
  while [ $((SECONDS - SINCE)) -lt $DELAY_S ]; do
    sleep 1
    if check-ip.sh "$CN" "$FB" >/dev/null; then
      echo "Check IP recovered."
      exit 0
    fi
  done
  echo "Check IP still fails. Updating DNS"
  update-dns.sh
  SINCE=$SECONDS
  while [ $((SECONDS - SINCE)) -lt $DELAY_S ]; do
    sleep 1
    if check-ip.sh "$CN" "$FB" >/dev/null; then
      echo "IP updated."
      exit 0
    fi
  done
  echo "Updating DNS failed"
  exit 1
fi