# Freedns init script

## drbrain9k

I just finished writing a freedns dynamic ip init script and figured I would share it. I would really love coding style comments, as it's my first script.

The idea is that my IPs only really change when I physically move my box. So I only need to check and update at boot time. If you want to check all the time, there are plenty of good cron scripts at http://freedns.afraid.org/scripts/freedns.clients.php that will suit.

The only thing you need to change is $UPDATE_URL in the /etc/conf.d/freedns file, and then use a simple rc-update to install it.

Here is the /etc/init.d/freedns file:

```
#!/sbin/runscript

# Copyright 2006 Jeffrey Nichols

depend() {

        need net

        use dns logger

}

checkconfig() {

        if [ -z "${CHECK_CMD}" ] ; then

                eerror "Please edit /etc/conf.d/freedns"

                eerror "I need to know how to check for IP!"

                return 1

        fi

        if [ -z "${OLDIP_FILE}" ] ; then

                eerror "Please edit /etc/conf.d/freedns"

                eerror "I need to know where to store the IP file!"

                return 1

        fi

        if [ -z "${UPDATE_COMMAND}" ] ; then

                eerror "Please edit /etc/conf.d/freedns"

                eerror "I need to know how to update the IP!"

                return 1

        fi

        return 0

}

start() {

        checkconfig || return $?

        ebegin "Getting current IP"

        CURRENTIP=`${CHECK_CMD}`

        echo "Found ${CURRENTIP}"

        if [ ! -e "${OLDIP_FILE}" ] ; then

                echo "Creating ${OLDIP_FILE}"

                echo "0.0.0.0" > "${OLDIP_FILE}"

        fi

        OLDIP=`cat ${OLDIP_FILE}`

        eend $? "Error reading old ip"

        if [ "${CURRENTIP}" != "${OLDIP}" ] ; then

                ebegin "Issuing update command"

                ${UPDATE_COMMAND}

                eend $? "Update failed"

        fi

        ebegin "Saving IP"

        echo "${CURRENTIP}" > "${OLDIP_FILE}"

        eend $? "Save failed"

}

```

and here is the /etc/conf.d/freedns file:

```
# /etc/conf.d/freedns

# Command to find the current IP on the system.

CHECK_CMD="/usr/bin/curl -s http://ip.dnsexit.com/ | sed -e 's/ //'"

# Where to store the file containing the old IP

# for comparison on the next run.

OLDIP_FILE="/var/lib/misc/oldip"

# The command to run when the IP is different.

UPDATE_URL="http://freedns.afraid.org/dynamic/update.php?" <--edit this line to the url from http://freedns.afraid.org/dynamic/index.php

UPDATE_COMMAND="/usr/bin/curl -s $UPDATE_URL"
```

Credit: I used the dynIP.sh file from http://freedns.afraid.org/forums/viewtopic.php?p=4161 for the idea and the ntp-client init files as a very loose template.

----------

## disi

That is what I use as a cron job on my system (copied a lot of yours  :Smile:  )

```
#! /bin/bash

# this script updates the dynamic ip on http://freedns.afraid.org/ using curl

# check http://freedns.afraid.org/api/ and as weapon ASCII for the phrase in UPDATE_URL

OLDIP_FILE="/var/lib/misc/oldip"

CHECK_CMD="/usr/bin/curl -s http://ip.dnsexit.com/ | sed -e 's/ //'"

UPDATE_URL="http://freedns.afraid.org/dynamic/update.php?<blablubbblablablablubbphrasesumber>"

UPDATE_COMMAND="/usr/bin/curl -s $UPDATE_URL"

echo "Getting current IP"

CURRENTIP=`${CHECK_CMD}`

echo "Found ${CURRENTIP}"

if [ ! -e "${OLDIP_FILE}" ] ; then

echo "Creating ${OLDIP_FILE}"

echo "0.0.0.0" > "${OLDIP_FILE}"

fi

OLDIP=`cat ${OLDIP_FILE}`

if [ "${CURRENTIP}" != "${OLDIP}" ] ; then

echo "Issuing update command"

${UPDATE_COMMAND}

fi

echo "Saving IP"

echo "${CURRENTIP}" > "${OLDIP_FILE}"
```

----------

