Files
useful-scripts/update-ipv6.sh

85 lines
1.9 KiB
Bash
Executable File

#! /bin/bash
PASSWORD=""
DOMAIN=""
INTERFACE="eth0"
FILENAME="/etc/haproxy/haproxy.cfg"
usage(){
cat << EOF
usage: update-ipv6 [OPTIONS] -p [PASSWORD] -d [DOMAIN_NAME]
Dynamic DNS script helper that updates records in freedns.afraid.org if the IPV6 address has been changed.
Options:
-f Force update in record even if IP hasn't change
--dry Shows if the IP addres has changed without updating it
-i Interface name default: eth0
-F Filename where to compare previous address default= /etc/haproxy/haproxy.cfg
EOF
exit 1
}
if [[ $# -lt 4 ]] ;
then
usage
fi
while [[ ! -z "$1" ]]; do
case $1 in
-p)
PASSWORD=$2
shift 2;;
-d)
DOMAIN=$2
shift 2;;
-f)
FORCE=true
shift 1;;
--dry)
DRY=true
shift 1;;
-i)
INTERFACE=$2
shift 2;;
*)
echo $option
usage;;
esac
done
addresses=$(/usr/sbin/ifconfig $INTERFACE | grep 'inet6.*global>' | awk '{ print $2 }');
GREAT=0
add=""
for address in $addresses;
do
secs=$(/usr/sbin/ip addr show dev $INTERFACE | grep -A1 $address | grep -v $address | awk '{ print $2}')
if [ $GREAT -gt ${secs/sec/} ] || [ $GREAT == 0 ];
then
GREAT=${secs/sec/}
add=$address
fi
done
echo $address
if [[ $add ]] && [[ -z $(grep $add $FILENAME ) ]] ; then
hadd=$(grep -o -m 1 'NAT1TO1=[^"]*' $FILENAME | awk 'BEGIN {FS = "=" } ; { print $2 }');
if [[ $DRY ]]; then
echo "Address would change from $add to $hadd"
exit 1;
fi
sed -i s/${hadd}/$add/ $FILENAME
echo curl --user "nextia365:$PASSWORD" -XPOST http://freedns.afraid.org/nic/update?hostname=$DOMAIN\&myip=$add
curl --user "nextia365:$PASSWORD" -XPOST http://freedns.afraid.org/nic/update?hostname=$DOMAIN\&myip=$add
else
echo Unchanged
if [[ $FORCE ]]; then
curl --user "nextia365:$PASSWORD" -XPOST http://freedns.afraid.org/nic/update?hostname=$DOMAIN\&myip=$add
fi
fi
if [[ $DRY ]]; then
echo "Address didn't change from $add"
exit 1;
fi