Instantly share code, notes, and snippets.

@corny /dynv6.sh
Last active Dec 11, 2018

Embed
What would you like to do?
Update script for dynv6.com to set your IPv4 address and IPv6 prefix
#!/bin/sh -e
hostname=$1
device=$2
file=$HOME/.dynv6.addr6
[ -e $file ] && old=`cat $file`
if [ -z "$hostname" -o -z "$token" ]; then
echo "Usage: token=<your-authentication-token> [netmask=64] $0 your-name.dynv6.net [device]"
exit 1
fi
if [ -z "$netmask" ]; then
netmask=128
fi
if [ -n "$device" ]; then
device="dev $device"
fi
address=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
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
if [ -z "$address" ]; then
echo "no IPv6 address found"
exit 1
fi
# address with netmask
current=$address/$netmask
if [ "$old" = "$current" ]; then
echo "IPv6 address unchanged"
exit
fi
# send addresses to dynv6
$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token"
$bin "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=auto&token=$token"
# save current address
echo $current > $file
@roman-1983

This comment has been minimized.

roman-1983 commented Oct 30, 2015

Would be great if this script has a "ipv4 only" option. In the moment it only works if you have a ipv6.

@pulsar256

This comment has been minimized.

pulsar256 commented Jan 15, 2016

@ComicZockt

This comment has been minimized.

ComicZockt commented Jan 15, 2016

Is there a php version or a batch version of this available?

@ghost

This comment has been minimized.

ghost commented Jan 16, 2016

Is there are reason why you use http for the update and not https which you have available?

@uhardegger

This comment has been minimized.

uhardegger commented Jan 25, 2016

I used the following php script on my synology webdrive (/sbin/dynv6ddns.php). In addition to the script i updated the /etc/ddns_provider.conf manually with the following provider configuration:

[USER_DynV6]                                                                                              
        queryurl=http://dynv6.com                                                                         
        modulepath=/sbin/dynv6ddns.php 

the dynv6ddns.php:

#!/usr/bin/php -d open_basedir=/sbin
<?php

if ($argc !== 5) {
    echo 'badparam';
    exit();
}

$account = (string)$argv[1];
$hostname = (string)$argv[3];
$ip = (string)$argv[4];

// check the hostname contains '.'
if (strpos($hostname, '.') === false) {
    echo 'badparam';
    exit();
}

// only for IPv4 format
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    echo "badparam";
    exit();
}

$url = 'https://dynv6.com/api/update?token='.$account.'&hostname='.$hostname.'&ipv4='.$ip;

$req = curl_init();
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($req);
curl_close($req);

$query='host updated';

if (substr($res,0,strlen($query)) === $query) {
    echo "good";
} else {
    echo $res.'     ';
    echo "update failed";
}
@zeroskunk

This comment has been minimized.

zeroskunk commented Jan 27, 2016

@danrl

This comment has been minimized.

danrl commented Jun 18, 2016

Looking for the mngtmpaddr flag ensures that no privacy extension addresses leak into dynamic DNS.

ip -6 addr list scope global mngtmpaddr $device

See https://gist.github.com/danrl/f568bf6d61acb3d0e9b11986b09deada

@lukasreindl

This comment has been minimized.

lukasreindl commented Aug 29, 2016

Hi, I am having some troubles with this script. I am totally new in scripting and dont know what to write in the console.
I tried this but it didn't work:
sh dynv6.sh token=Q-4pOEg4UyAuq8mySeWgAJfiGWXHtQ ./dynv6.sh test123x.dynv6.net

What should I make different?

@bertenvdb

This comment has been minimized.

bertenvdb commented Sep 5, 2016

@lukasreindl

  1. Click on the 'raw' button on the top right-hand side of the above script, and copy/paste the entire code to a file dynv6.sh in your home directory.
  2. Make the script executable
    chmod +x dynv6.sh
  3. Execute the script using the link found on the 'instructions' tab of your host on dynv6.net
    token=Q-4pOEg4UyAuq8mySeWgAJfiGWXHtQ ./dynv6.sh test123x.dynv6.net

Optionally have cron execute the script every 5 mins or so. Nothing will be sent if your IP hasn't changed.

  • crontab -e to open your editor
  • Add the line */5 * * * * token=Q-4pOEg4UyAuq8mySeWgAJfiGWXHtQ ./dynv6.sh test123x.dynv6.net
  • save and close editor
@wizkoder

This comment has been minimized.

wizkoder commented Feb 15, 2017

I run the script on my raspberry pie. It runs fine and updates my ipv4 and ipv6 address.
When I enter the host name at home in my browser everything works. I can login via ssh.
But when I do the same from my machine at work or from my mobile phone with turned off wlan I cannot reach the pie.
Any idea what could be wrong? Does it maybe try to connect via ipv4? The address is set but it will not be able to reach my machine as
I have a dual-stack here.

@kdm6389

This comment has been minimized.

kdm6389 commented Feb 21, 2017

thanks for both ipv6 and ipv4

@rolf-epking

This comment has been minimized.

rolf-epking commented Mar 14, 2017

If IPv6 address not changed, but the (global) IPv4 address, it wont be updated..

@aolinger

This comment has been minimized.

aolinger commented Apr 18, 2017

Hi... ip -6 command used in line 19 not working in mac os 10.12 Sierra?!! (indeed not...) I get an error "command not found"

@chbardel

This comment has been minimized.

chbardel commented Apr 25, 2017

Is it possible to update the records and not the normal domain name? I tried to just append it:
token=xxxxxxxxxxxxx ./dynv6.sh yyyyyy.xxxxxx.dynv6.net

It produced the following error:
curl: (22) The requested URL returned error: 404 Not Found

Would be nice to get a Inadyn-mt example configuration as well for Dynv6 Records. Couldnt find one online.

@stoeffel85

This comment has been minimized.

stoeffel85 commented Aug 9, 2017

Hi, im running this on a raspberry Pi behind my ISPs router. Is there a way to use this script to update the external/public IP?

@nephilim75

This comment has been minimized.

nephilim75 commented Aug 10, 2017

@stoeffel85

Hi, im running this on a raspberry Pi behind my ISPs router. (...)

Is it not working? What error is coming up? Would you please send some more information, please?

Kind regards
//neph

@stoeffel85

This comment has been minimized.

stoeffel85 commented Aug 15, 2017

Hi nephilim75,
the script is working nicely, however, the IP that is updated to dyn6c is the raspberrys local ip in my home networt (192.168.0.something).
However, I found a way, I substituted line 19 with
v4_address=$(curl ifconfig.me)

@urbansciences

This comment has been minimized.

urbansciences commented Jun 23, 2018

I am running this script on my Raspberry Pi 3.

ifconfig -a
pi@raspberrypi:~ $ ifconfig -a
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether b8:27:eb:49:d4:40 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.15 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::b6bc:2ed3:e647:93b9 prefixlen 64 scopeid 0x20
ether b8:27:eb:1c:81:15 txqueuelen 1000 (Ethernet)
RX packets 290 bytes 25171 (24.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 211 bytes 38365 (37.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

When I run the script, I get this output:

pi@raspberrypi:~ $ token= ./dynv6.sh satvam.dynv6.net
no IPv6 address found

Am I doing something wrong here?

@Moonbase59

This comment has been minimized.

Moonbase59 commented Jun 27, 2018

The IPv6 "address=…" line gives me the global temporary dynamic IPv6 host address, but what I need for DNS updates is the global dynamic (i.e., MAC-based) host IP, so I use a modified line like this:

address=$(ip -6 addr list scope global permanent dynamic $device | grep -v " fd" | grep -v " temporary" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
@nferruzzi

This comment has been minimized.

nferruzzi commented Aug 28, 2018

for mac

address=$(ifconfig | grep inet | grep "autoconf temporary" | sed -E -n 's/.*inet6 ([0-9a-f:]+).*/\1/p' | head -n 1)
@blub4747

This comment has been minimized.

blub4747 commented Oct 9, 2018

When I am trying to run

token=Q-4pOEg4UyAuq8mySeWgAJfiGWXHtQ ./dynv6.sh test123x.dynv6.net
With my own Token and hostname I get the following error.

sed: -e expression #1, char 10: unterminated `s' command
no IPv6 address found
Do I need to add a '-e' and If yes. Where do I please need to place it?

Thx,blub4747

@CyBiS

This comment has been minimized.

CyBiS commented Oct 28, 2018

@uhardegger Thanks a lot for sharing your script! I've modified it a bit for my Synology router and added system log prints (to /var/log/messages). The modified version can be found here. The corresponding ddns_provider.conf file is uploaded here.

@Beaving

This comment has been minimized.

Beaving commented Dec 10, 2018

#!/bin/bash

#Configure
Hostname="example.dynv6.net"
IPVERSION="6" #IPV4=4, IPV6=6
UpdateURL="http://dynv6.com/api/update?hostname=$Hostname&ipv6=auto&token=YOUR_TOKEN"

RecordType=$([ "$IPVERSION" == 4 ] && echo "A" || echo "AAAA")

while true; do
 SavedIP=$(dig -$IPVERSION +short $RecordType $Hostname) #check IP which is stored on dynamic DNS
 ActualIP=$(dig -$IPVERSION +short $RecordType myip.opendns.com @resolver1.opendns.com) #get actual IP

 if [ "$SavedIP" != "$ActualIP" ] ; then
	wget -qO- $UpdateURL &> /dev/null
	if [[ $? -ge 0 ]]; then
	 SavedIP=$ActualIP
	fi
 fi

 sleep 5s #run every 5 seconds
done

Very simple and neat script that runs every 5 seconds in a loop. Has some advantages over the script above. Should work on most clients, includes a reliable way to get the IP and can be adapted to most dynamic DNS providers.

Configure hostname, record type and update URL, done.

For dynv6 you have to add your token in the URL. If you want to use IPV4, change IPVERSION to 4 and change url to ipv4 version of the URL. Then, simply copy it into a file like ddnsupdate.sh, chmod +x ddnsupdate.sh to make it executable and execute it from a startup script like rc.local: nohup /home/ddnsupdate.sh > /dev/null 2>&1&

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment