Category Archives: English

Checking expiry dates of SSL certificates

Once again I missed the expiry date of one of the SSL certificates on my server. Therefore I am now using a cronjob to warn me early enough that a certificate is about to expire.

This is the script /usr/local/bin/ssl-cert-check which checks the expiry date of the certificate files passed as arguments:

#!/bin/bash
 
DAYS=30
 
for file in "$@"; do
    openssl x509 -checkend $(( 86400 * $DAYS )) -in "$file" > /dev/null
    if [ $? != 0 ]; then
        echo "==> Certificate $file is about to expire soon:"
        openssl x509 -enddate -in "$file" -noout
    fi
done

And the corresponding cronjob entry checking SSL certificates once a day:

MAILTO=root
6       6    * * *  nobody  /usr/local/bin/ssl-cert-check /etc/apache2/ssl/*.crt /etc/ssl/certs/dovecot.pem

pastie.org shell script

I wrote a bash script to create new pastes on pastie.org. It features automatic source language selection based on the file extension and has a switch to make a private paste.

$ pastie --help
Usage: pastie [options] [files...]

Options:
    -h, --help          display this help
    -l, --lang    set language of the paste
    -p, --private       make paste private

If --lang is not specified, this script will try to determine the type of each
file automatically based on the extension. If no files are given on the
command line it reads from standard input.

You can download it here:
http://pastie.org/904797

The script is public domain, so do whatever you want with it.

Although this is the initial release, I bumped the version number to 1.6 already. During testing the script I pasted itself several times to pastie.org. I set a arbitrary higher version number to avoid confusion in case the previous pastes ever turn up in Google or wherever.

Update:
Seems like their parser for shell is a bit broken and doubles the heredoc starting and ending sequences in the output. For whatever reason it appears as “<<END<<END”. Please use the raw version for download instead of copy & paste.

Calculating sum of numbers in the shell

More than one time I wanted to sum up numbers in the shell.

Imagine a command producing a list of numbers like this:


$ ...
42
23
966
1764
529
4711

Now calculating the sum of these numbers is not trivial. Using tools like bc or calc directly is not possible because you would need to put plus signs between the numbers first. So you could probably replace all ‘\n’ with ‘+’ with tr — but not the last one as that would lead to a syntax error later. Or as an alternative you could write a long shell construct with while read ... etc. In short this is just getting way too complicated for such a simple task.

Here is the simple solution in awk:


$ ... | awk 'BEGIN {total = 0} {total += $1} END {print total}'
8035

bzr shelve improved in 2.1.0

Sometimes I was a bit frustrated that bzr does not support hunk splitting during selection changes with bzr shelve, as does git commit --interactive. Now with bzr 2.1.0 there is a new option available to launch an external editor to shelve changes as you like.

Just add the option change_editor to your ~/.bazaar/bazaar.conf in the [DEFAULT] section.

For example:


[DEFAULT]
change_editor = vimdiff -f @new_path @old_path

Alternatively, you can of course use meld or kdiff3 as well as change_editor, which will be a lot easier to use for beginners.

The placeholder @new_path will automatically be replaced with the path of the new version and @old_path with the path of the old version. With this configuration, there will be a new choice “e” during shelve:

...
Shelve? [yNefq?]

Entering “e” will start vimdiff in a two column view with the new version to the left and the old version to the right. You should read :help diff in vim to get more information about this mode. Basically you can obtain changes from the other buffer using do (“diff obtain”) and put changes to the other buffer using dp (“diff put”). Note that the old version on the right will be read-only and you are not supposed to change it. Also do not make unrelated changes or introduce new differences as that will result in conflicts on bzr unshelve later.

When you are fine with the changes exit the editor with :wqa. After confirming the shelve once more, you will see the left file as the new version in the working tree and every difference to the right file will be shelved.

Unfortunately this is not yet documented in bzr itself at the time of this writing.