2012-12-07

Swedish post tracking cron job

/usr/local/bin/post-check
#!/bin/bash

# Script arguments
# argument 1 - package number, i.e. 012345670001234567
# argument 2 - mail address for package status updates
nr=$1
to=$2

# Swedish post perma-link
link="http://server.logistik.posten.se/servlet/PacTrack"
link="$link?lang=GB&xslURL=/xsl/pactrack/standard.xsl&kolliid=$1"

# Script saves status updates into temp dir
cd /tmp/

# Send the mail
# argument 1 - subject
# argument 2 - message
function send_mail() {
 # arg1 subject
 echo "$2" | mail -s "$1" \
    -a "MIME-Version: 1.0" \
    -a "Content-Type: text/html" \
    "$to"
}

# Download package latest status
wget "$link" -q -O post-status.new
if [ $? -ne 0 ]; then
 error="Could not download package status."
 echo "$error Mail sent."
 send_mail "Package $nr status error" "$error"
 exit
fi
# Make sure both current and previous status file exists
touch post-status.new
touch post-status.old

# Check if there is a difference in two last status sizes
new_size=$(stat -c%s post-status.new)
old_size=$(stat -c%s post-status.old)
if [ $new_size -ne $old_size ]; then
 echo "Package status updated. Mail sent."
 send_mail "Package $nr status updated" "`cat post-status.new`"
fi

# Save the status
cp post-status.new post-status.old

crontab -e
*/15 * * * * /usr/local/bin/post-check 012345670001234567 your@mail.com

2012-08-10

Lua output redirection

While ago I was working on Lua based implementation of the scripting library. One of the requirements was a total control of output and error messages, to allow it's automatic handling. One of the issues I've faced is that some output was going to stdout even if there is redefined print Lua function.

I thought about two quick solutions:
- redirect standard streams;
- redefine printf and it's adjacent functions.

Since standard streams, both stdout and stderr, in my case were already redefined (for remote debugging purposes), I didn't had much to choose from.

Looking into Lua 5.1 sources, I've found that stdout was simply hard-coded in some places. During up-streaming this patch to Lua 5.2 surprisingly most of the hard-coded stream names went away, and it looks like it would be possible to use redefined print without losing anything. I haven't tested this though. Following just works for Lua 5.1.x and above.

2012-08-08

Manually adding missing apt public keys

When apt-get update starts complaining about missing public keys, it's always pain in the ass when you're behind proxy and gpg --recv-keys doesn't work. Following is the manual way of adding missing apt public keys:

1. Obtain public key via browser:
http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x[KEY]
2. Save the key.
3. Import the key file in System->Administration->Software Sources

2012-06-12

How-to add a permanent network interface alias on Ubuntu 10.04 and higher

1. Add network interface alias by adding following into /etc/network/interfaces:
# Alias interface
auto eth0:0
iface eth0:0 inet static
  address 192.168.1.1
  netmask 255.255.255.0

2. Create a new file and place it in /etc/network/if-up.d/:
#!/bin/bash

if [ "$IFACE" = "eth0" ]; then
        /sbin/ifup --force eth0:0
fi

3. Restart network via Network Manager or command line:
sudo /etc/init.d/networking restart