Helping a mate out setting up an embedded system to report to his GNUDIP server. At one stage, we weren’t trusting the ez-ipupdate code (which by the way, is horrible).

In any case, did you know you can do some simple sockets programming from BASH? No? Me neither, until now…. it turns out if you try to open a file at /dev/tcp/HOSTNAME, BASH will open a socket for you…. Not that you should really do this, unless all you have is busybox…

So for anyone who wants it, here’s a gnudip client written in pure bash:

#!/bin/bash
GNUDIP_HOST=""
GNUDIP_PORT=3495
GNUDIP_USER=""
GNUDIP_PASS=""
GNUDIP_DOMAIN=""
GNUDIP_IFACE="eth0"
# Get the interface IP
iface_ip=`ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1`
# Bind a tcp socket
exec 3/dev/tcp/$GNUDIP_HOST/$GNUDIP_PORT
# Read off the salt
read -u 3 salt
# Hash the password with the salt
hash_string=$GNUDIP_PASS
hash=`echo "$hash_string" | md5sum | cut -d " " -f 1`
hash_string="$hash.$salt"
hash=`echo "$hash_string" | md5sum | cut -d " " -f 1`
# Generate the update string
pdate_string="$GNUDIP_USER:$hash:$GNUDIP_DOMAIN:0:$iface_ip"
echo $pdate_string >&3
# Read off the response
read -u 3 resp
if [ $resp -eq 0 ]; then
    echo "Updated!"
else
    echo "Error: $resp"
fi

Yeah…. that’s right. Only, I’m not 100% sure that this code works, it keeps returning a failure - I’m suspicious of the gnudip server is broken!

Also for those interested, heres a version that works with the newer HTTP protocol interface:

#!/bin/bash
GNUDIP_URL="http://example.com/gnudip/cgi-bin/gdipupdt.cgi"
GNUDIP_USER=""
GNUDIP_PASS=""
GNUDIP_DOMAIN=""
GNUDIP_IFACE="eth0"
# Get the interface IP
iface_ip=`ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1`
# Get salt details
TMPFILE="/tmp/gnudip_$$"
wget -O $TMPFILE --quiet $GNUDIP_URL
token_salt=`grep meta $TMPFILE | grep salt | cut -d '"' -f 4`
token_time=`grep meta $TMPFILE | grep time | cut -d '"' -f 4`
token_sign=`grep meta $TMPFILE | grep sign | cut -d '"' -f 4`
rm -f $TMPFILE
# Hash the password
hash=`echo "$GNUDIP_PASS" | md5sum | cut -d " " -f 1`
hash=`echo "$hash.$token_salt" | md5sum | cut -d " " -f 1`
# Generate the update URL
url="$GNUDIP_URL?salt=$token_salt&time=$token_time&sign=$token_sign&user=$GNUDIP_USER&domn=$GNUDIP_DOMAIN&pass=$hash&reqc=0&addr=$iface_ip"
# Visit Page
wget -O $TMPFILE --quiet $url
token_retc=`grep meta $TMPFILE | grep retc | cut -d '"' -f 4`
token_addr=`grep meta $TMPFILE | grep addr | cut -d '"' -f 4`
rm -f $TMPFILE
# Output the result
if [ $token_retc -eq 0 ]; then
    echo "Success!"
else
    echo "Error: $token_retc"
fi