diff --git a/4-public-ip.sh b/4-public-ip.sh index 8347d17..1538e1e 100755 --- a/4-public-ip.sh +++ b/4-public-ip.sh @@ -1,8 +1,8 @@ #!/bin/bash -# does not always work, when you have an IPv6 connection -#dig +short A myip.opendns.com @resolver1.opendns.com +# works consistently if you specify the -4 to force IPv4 +dig -4 +short A myip.opendns.com @resolver1.opendns.com -curl https://ipinfo.io/ip -echo "" +#curl --fail --silent --show-error https://ipinfo.io/ip +#echo "" diff --git a/6-public-ip.sh b/6-public-ip.sh index 9584d55..9bd7b32 100755 --- a/6-public-ip.sh +++ b/6-public-ip.sh @@ -1,4 +1,5 @@ #!/bin/bash -dig +short AAAA myip.opendns.com @resolver1.opendns.com +# works consistently if you specify the -6 to force IPv6 +dig -6 +short AAAA myip.opendns.com @resolver1.opendns.com diff --git a/dynv6.sh b/dynv6.sh new file mode 100755 index 0000000..7dfa1b0 --- /dev/null +++ b/dynv6.sh @@ -0,0 +1,85 @@ +#!/bin/sh + +hostname=$1 +device=$2 + +file4="$HOME/.dynv6.addr4" +file6="$HOME/.dynv6.addr6" + +[ -e $file4 ] && old4=`cat $file4` +[ -e $file6 ] && old6=`cat $file6` + +# ensure we have required information +if [ -z "$hostname" -o -z "$token" ]; then + echo "Usage: token= [netmask=64] $0 your-name.dynv6.net [device]" + exit 1 +fi + +# [device] would be something like "eth0" or "wlan0" +if [ -n "$device" ]; then + device="dev $device" +fi + +# what command should we use to call the API +if [ -e /usr/bin/curl ]; then + bin="curl -fsS" +elif [ -e /usr/bin/wget ]; then + bin="wget -O-" +else + echo "neither curl nor wget found" + exit 1 +fi + +# what command should we use to determine IPv4 +if [ -e /usr/bin/dig ]; then + lookup4="dig -4 +short A myip.opendns.com @resolver1.opendns.com" +else + lookup4="$bin https://ipinfo.io/ip" +fi + +# lookup IPv4 +ipv4=`$lookup4` +if [ -z "$ipv4" ]; then + echo "failed to lookup IPv4 address, continuing with 'auto'" + ipv4="auto" +fi + +# calculate IPv6 +address6=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1) + +if [ -z "$address6" ]; then + echo "no IPv6 address found" + exit 1 +fi + +if [ -z "$netmask" ]; then + netmask="128" +fi + +ipv6="$address6/$netmask" + +# update IPv4, if changed +if [ "$old4" = "$ipv4" ]; then + echo "IPv4 address unchanged" +else + echo -n "Updating IPv4: " + $bin "https://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=$ipv4&token=$token" + echo "" + + # save current address + echo $ipv4 > $file4 +fi + +# update IPv6, if changed +if [ "$old6" = "$ipv6" ]; then + echo "IPv6 address unchanged" +else + # send addresses to dynv6 + echo -n "Updating IPv6: " + $bin "https://ipv6.dynv6.com/api/update?hostname=$hostname&ipv6=$ipv6&token=$token" + echo "" + + # save current address + echo $ipv6 > $file6 +fi +