Debugging Scripts

A script that is executed by an application or another script is failing. Executing the script manually doesn’t provide more clues since it’s only failing when invoked by another program. Also, you don’t want to redirect the output of the script each time you execute it. And you want to see more debugging information like line numbers.

Adding the following lines at the beginning of the script will help:

export PS4='$0.$LINENO+ '
exec > /tmp/script.log
exec 2>&1
set -x

Example:

$ cat test
#!/bin/bash
export PS4='$0.$LINENO+ '
exec > /tmp/script.log
exec 2>&1
set -x
ls -ld /etc
ls -ld /boot
echo "This is a test"
$ ./test
$ cat /tmp/script.log
./test.6+ ls -ld /etc
drwxr-xr-x 83 root root 7512 2006-07-22 16:49 /etc
./test.7+ ls -ld /boot
drwxr-xr-x 5 root root 1960 2006-07-22 15:30 /boot
./test.8+ echo 'This is a test'
This is a test
$

These lines will turn on debugging and all information will be redirected to the log file. So you won’t have to redirect the output each time you run the script, e.g. “./script > /tmp/script.log 2>&1″. In some cases you can’t do that if the script is invoked by an application.

The PS4 builtin shell variable describes the prompt seen in debug mode. The $0 variable stands for the name of the script file itself. $LINENO shows the current line number within the script. The exec command redirects I/O streams. The first exec command redirects stdout stream 1 to /tmp/script.log. 2>&1 redirects stderr stream 2 to stdout stream 1. And “set -x” enables debugging.

Dynamic DNS Setup

  1. Generating Secure DNS Keys
  2. On the home/client machine:

    # mkdir /etc/bind/tsig

    # cd /etc/bind/tsig

    # dnssec-keygen -a HMAC-MD5 -b 128 -n HOST host.domain.tld.

    Note the “.” after the tld. This generates the public and the private keys.

  3. named.conf
  4. On the remote server:

    Edit “/etc/named.conf” and add the generated key to the conf. (Note the trailing dot):

    key host.domain.tld. {

    algorithm hmac-md5;

    secret "qUSfVtkYf7WLxiZaOTN3Ua==";

    };
  5. Grant Authority
  6. Still on the remote server:

    Edit the “/etc/bind/zone.domain.tld” file, and modify the current allow-update line to include the key.

    allow-update   { key "default_key."; key "host.domain.tld."; };

    This allows full authority to modify any record within the domain (Be Warned).

    Restart named and make sure nothing is broken.

  7. nsupdate
  8. Back to the client machine:

    Run nsupdate to test that the client can now make updates.

    # nsupdate -k /etc/bind/tsig/Khost.domain.tld.*.key

    > update delete host.domain.tld A

    > update add host.domain.tld. 600 A 1.2.3.4

    > send

    > quit

    It first deletes host.domain.tld if it already exists, then recreates it with the given TTL, type, and IP address. The TTL is the time-to-live, which is a value used by other DNS servers to determine how often they refresh the entry for this host. A smaller values means they’ll refresh more often, which is what you want for a dynamic entry. “send” tells nsupdate to send the updates to the server.

  9. Automate
  10. Create a script and put it in a 10 minute cron to check for changes in the wan ip address and run nsupdate automagically.

    # cat /etc/cron.d/ddns

    SHELL=/bin/sh

    */10 * * * * root /etc/bind/ddns

    Below is an example script that gets the info from a Belkin wireless router within the home lan.

    #!/bin/bash

    # ddnsHOSTNAME="host.domain.tld"

    KEYFILE="/etc/bind/tsig/Khost.domain.tld.*.key"

    TTL=600

    #LOG="/tmp/ddns_log"

    LOG="/dev/null"

    IP_FILE="/tmp/ddns_ip"

    NEW_IP=`wget -q -O - 192.168.2.1 | grep "Up.*dw" | tr "n" " " | awk -F "'" '{print $12}'`

    function do_nsupdate {

    echo "New IP address (${NEW_IP}) found. Updating..." >> $LOG

    echo $NEW_IP > $IP_FILE

    nsupdate -k $KEYFILE >> $LOG
    update delete $HOSTNAME A

    update add $HOSTNAME $TTL A $NEW_IP

    send

    quit

    EOF

    }

    if [ ! -f $IP_FILE ]; then

    echo "Creating $IP_FILE..." >> $LOG

    do_nsupdate

    else

    OLD_IP=`cat $IP_FILE`

    if [ "$NEW_IP" = "$OLD_IP" ]; then

    echo "new and old IPs (${OLD_IP}) are same. Exiting..." >> $LOG

    exit 0

    else

    do_nsupdate

    fi

    fi

    exit 0

Installing A Linux Distro to An USB Drive


Let me begin with emphasizing what is an USB device. Generally, it will be an USB stick, but it could be as well an ordinary MP3 player, an iPod, an external HDD, a Smartphone, a PDA or who knows what else. The important thing is that it acts as a storage device. It is important that you u … [read more >>]

Iptables Basic Guide


People always complain about how hard iptables are to understand and configure. I’m not saying it’s an easy process but once you get the hang of it, it should seem a little less difficult. Iptables is a user space tool which is used to create rules for packet filtering and NAT modul … [read more >>]

OpenSUSE 10.2 Installation Guide with Screenshots

Easy steps to a complete Linux workstation.

So, you have decided to make the first steps into the Linux world, and you’re asking yourselves: “What distribution should I try?” Even though I guess you’ve already heard about the … [read more >>]

openSUSE Survey Results

The openSUSE Survey results are in: thanks to the over 27k people who participated. And over 70% of those use the best desktop environment!

Download Results (pdf)


Core GNU/Linux 2.0 Released!

Core is a minimal distribution of the GNU/Linux operating system designed to be the basis for a complete system constructed by the end user. A fresh installation of Core (which will take approximately 15 minutes) will boot into a console and provide the user with the tools needed to download, compile and install other applications. Core contains nothing beyond what is required to perform these tasks. Core is primarily designed for experienced Linux users, though it has found an audience with those looking to learn about the internals and operation of a Linux system.

Four years ago, on April 4, 2003, Josh Devon released Core 1.x. On May 1, 2007, the new Core development team released a fully functional beta of Core 2.0. This beta has been in constant use for development for a number of weeks, and is completely stable in that capacity.

Check out the Core GNU/Linux website and SourceForge project for more information and downloads. We look forward to your feedback and ideas on the forum or on IRC at #coredistro on irc.freenode.net.

Follow

Get every new post delivered to your Inbox.