Pages

July 02, 2008

An inadyn wrapper

Inadyn is a lightweight command-line tool for updating dynamic DNS addreses. Its use is supported by DynDNS, FreeDNS, and others. Binaries are available for Windows and Linux, and Debian and Ubuntu both have inadyn packages. Overall it seems like a good choice for a dynamic DNS update client.

In Linux, administrators usually set inadyn up to run as a daemon. But for regular desktop use this might present some problems. Most notably, if a network connection is not available when inadyn starts, it just exits. Another problem is that setting up a daemon can be a little scary for new users.

So I came up with a little script that can be run at login that launches inadyn not as a daemon but as a regular process. If the process is already running, it will warn the user. If the process didn't start, it will warn the user. If everything goes ok, it lets the process run and quietly retires.

You will have to hand-edit the 'inadynOptions' variable below to make it suit your needs. The whole thing looks like (you may need to copy/paste the text into a text editor if the left margins are cropped):
#! /bin/bash
# (c) 2008 Mithat Konar
# 2008-07-25

# Try to start an inadyn process. If it starts and doesn't quit right away
# (perhaps because the network isn't available) exit the script leaving the
# inadyn process running. Otherwise, prompt the user that it's not running
# and give the user the chance to try again or quit.

# Requires zenity

# =========
# constants
# =========
# use absolute paths only ... relative paths and env variables not tested! really!!
inadynOptions="--input_file /home/mithat/.inadyn/inadynAfraid.conf" # options to pass to inadyn

timeBetweenTries=20s   # time between attempts to restart the process

# ==========
# UI strings
# ==========

uiAlreadyRunningTitle="Inadyn already running?"
uiAlreadyRunning="It looks as though
inadyn $inadynOptions
is already running.

Shall I force it to start anyway?"

uiNotRunningTitle="Inadyn not running"
uiNotRunning="Inadyn isn't running, possibly becuase the network isn't up.

Try again?"

# =========
# functions
# =========

# tryToStart() will try to start the process "inadyn $inadynOptions"
# it does not check to see if the process is running
# requres global variables 'timeBetweenTries' and 'inadynOptions'.
# writes the PID of the process to global variable 'PID'.
# if startup was successful, sets global variable 'isRunning' to
# process information, otherwise isRunning will be blank.
function tryToStart()
{
/usr/sbin/inadyn $inadynOptions &   # try to start it
PID=$!     # capture the process ID
sleep $timeBetweenTries   # give it some time
isRunning=`ps aux | grep "inadyn $inadynOptions" | grep "$PID" | grep -v grep` # check
}

# ======
# "main"
# ======

isRunning=`ps aux | grep "inadyn $inadynOptions" | grep -v grep`
if [ -n "$isRunning" ] ; then
zenity --question --title "$uiAlreadyRunningTitle" --text "$uiAlreadyRunning"
if [ $? -eq 1 ] ; then
echo -e "$0: User cancelled"
exit 1
fi
fi

tryToStart    # try to start the process

while [ -z "$isRunning" ] ; do  # while the process isn't running ...
zenity --question --title "$uiNotRunningTitle" --text "$uiNotRunning"
if [ $? -eq 1 ] ; then
echo -e "$0: User cancelled"
exit 1
fi
tryToStart   # try to start the process
done

Be sure to set the file in which you save the script to be executable (using chmod or your file manager).

You will also need to make /usr/sbin/inadyn executable by regular users. In a terminal:
sudo chmod u+s /usr/sbin/inadyn

To start the script in Xfce, open "Autostarted apps" and add an entry for
bash -c "<path to the script>"

To start the script in IceWM, add
bash -c "<path to the script>" &
to your ~/.icewm/startup file (don't forget the & at the end of the line!). Adding just
<path to the script> &
seems to work as well.

1 comment:

Mithat said...

I made some changes to make the script work without being called as root.