#!/usr/local/bin/bash
# Neato Yi.org thingy - For dynamic IP's.
# (C) Dragon Knight <dragonk@mato.com>  --  My 2nd shell script BTW, enjoy.
# This script produces an error under sh, also, please suggest improvements,
# I know it could be better.
# It is used on my box in conjunction with the shell script provided by
# yi.org, !bg /etc/ppp/dns_update.sh in my ppp.linkup, and this shell
# script run every X minutes with cron. -- FreeBSD 3.3-Stable
# 0,10,20,30,40,50 * * * * /etc/ppp/dragonk.yi.org.sh
# This way if for some reason the initial dns update fails, for example
# you connect, disconnect, then reconnect in short order you're dns
# will still get updated.


# Set the name of your external interface, yi.org username and password here
extern=tun0
user="username"
pass="password"



amiconn=`netstat -rn | grep default | sed 'y: :\n:' | head -n 1`

if [ -z $amiconn ]; then
 echo "No default route, cannot update"
 exit 0

        elif [ $amiconn == "default" ]; then
         echo "Interface looks like it might be up"
fi
  ifup=`netstat -rn | grep default | awk '{printf($2)}';echo`
  /sbin/ping -Q -c 4 $ifup 2>/tmp/1.ip >/tmp/1.ip
  ifupchk=`cat /tmp/1.ip | tail -n 2 | awk '{printf($7)}';echo`


if [ $ifupchk == "100%" ]; then
 echo "All packets lost. interface is down, cannot update"
 exit 0

        elif [ $ifupchk == "0%" ]; then
         echo "All packets got through, proceeding"


else
 echo "Interface is down, cannot update"
  exit 0
fi

devip=`/sbin/ifconfig $extern | grep inet | grep ff00 | awk '{printf($2)}';echo`
echo "IP of interface is $devip"

hostip=`/usr/bin/host ${user}.yi.org | grep address | awk '{printf($4)}';echo`
echo "IP that ${user}.yi.org resolves to is $hostip"

if [ "$devip" == "$hostip" ]; then
echo "IP's are matching, no need to update"
        exit 0
else

#date

/usr/local/bin/wget -q -O /tmp/fetch_results.html \
--http-user=$user --http-passwd=$pass \
http://www.yi.org/bin/dyndns.fcgi?ipaddr=$devip

fi

