Last active
February 7, 2018 21:35
-
-
Save Hornet-C/d018303f4d629789fd468227c8c0bdf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
# based on https://gist.github.com/corny/7a07f5ac901844bd20c9 | |
# updates the (public) IPv6 address and the (local?) IPv4 address | |
# cp ./dynv6.sh /usr/local/bin/ | |
# chmod +x /usr/local/bin/dynv6.sh | |
# sudo touch /var/log/dynv6 | |
# sudo chown $USER /var/log/dynv6 | |
# | |
# ADD A CRONJOB (crontab -e) | |
# optional: add ' MAILTO="" ' to crontab if you whish no eMail alert | |
# @reboot /usr/local/bin/dynv6.sh <public_dns> <token> [device] >/var/log/dynv6 2>&1 | |
# */20 * * * * /usr/local/bin/dynv6.sh <public_dns> <token> [device] >/var/log/dynv6 2>&1 | |
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin | |
hostname=$1 | |
token=$2 | |
device=$3 | |
dynv6Path=$HOME/.dynv6 | |
mkdir -p $dynv6Path | |
file6=$dynv6Path/addr6 | |
file4=$dynv6Path/addr4 | |
[ -e $file6 ] && old6=`cat $file6` | |
[ -e $file4 ] && old4=`cat $file4` | |
if [ -z "$hostname" -o -z "$token" ]; then | |
echo "Usage: $0 <your-name.dynv6.net> <your-authentication-token> [device]" | |
exit 1 | |
fi | |
if [ -z "$netmask" ]; then | |
netmask=128 | |
fi | |
address6=$(ifconfig $device | grep inet6 | awk -F " " '{print $2}'|tail -n 1) | |
address4=$(ifconfig $device | grep inet | awk -F " " '{print $2}'|tail -n 1) | |
if [ -e /usr/bin/curl ]; then | |
bin="curl -fsS" | |
elif [ -e /usr/bin/wget ]; then | |
bin="wget -O-" | |
else | |
echo "$(date): neither curl nor wget found" | |
exit 1 | |
fi | |
if [ -z "$address6" ]; then | |
echo "$(date): no IPv6 address found" | |
exit 1 | |
fi | |
# address with netmask | |
current6=$address6/$netmask | |
current4=$address4 | |
if [ "$old6" = "$current6" ]; then | |
echo "$(date): IPv6 address unchanged: $current6" | |
else | |
$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current6&token=$token" | |
echo "$(date): New IPv6 address: $current6" | |
fi | |
if [ "$old4" = "$current4" ]; then | |
echo "$(date): IPv4 address unchanged: $current4" | |
else | |
$bin "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=$current4&token=$token" | |
echo "$(date): New IPv4 address: $current4" | |
fi | |
# save current address | |
echo $current6 > $file6 | |
echo $current4 > $file4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment