Python example : Test script / module using regex

This is a simple piece of code to test if sys.argv[1] matches a file type using a regex to match a specific file type, in this case *.flac (it doesn’t actual check to see if the file is an actual Flac file, just that it is named correctly).

Here is the code with plenty of comments.

Also included (commented out) is the logic to match a date string.

Happy coding.

#!/usr/bin/env python3
# Author :, @shallawell
# A test function to check a if argv contains *.flac
# Filesname:
# Description:

# ensure PYTHONPATH is set properly
import sys,os,os.path

import re

def FLACfileTest():
    # Regex for *.flac
    pattern = r"([a-zA-Z]?\.flac)"
    # Regex for Date
    #pattern = r"([a-zA-Z]+) (\d+)"
    input = str(sys.argv[1])
    #input = "June 24"
#    input = "brian.mp3"  # FALSE condition
    #input = "brian.mp3.flac"  # TRUE condition
    if, input):
        match =, input)
        #match =, input)
    # This will print [0, 7), since it matches at the beginning and end of the
    # string
    #print "Match at index %s, %s" % (match.start(), match.end())
    # So this will print "June 24"
    #print "Full match: %s" % (
        print "File ends with FLAC.. runs ok %s" % (
        return 0 # pass
    # So this will print "June"
    #print "Month: %s" % (
    # So this will print "24"
    #print "Day: %s" % (
    # If does not match, then None is returned
        print("Error: Must be a FLAC file .. your file was " + sys.argv[1])
        return 1 # fail

# make the file usable as a script as well as an importable module
if __name__ == "__main__":
    import os, fnmatch
    # execute function

#version info
__version__ = '0.1'

To run

# python brian.mp3  # fail
# python brian.mp3.flac  # pass

Go to my Git repository to get the source source source


Containers – Docker on Ubuntu 14.04

Update – February 11, 2017 – Added Cleaning up docker section.


  • OS: Ubuntu 14.04
  • sudo access to root
  • internet access

Install Docker

Update package manager, install the package & create a symlink from to docker, fix bash auto complete to add ‘docker’

sudo apt-get update
sudo apt-get -y install
sudo ln -sf /usr/bin/ /usr/local/bin/docker
sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker

Start Docker on system start-up

update-rc.d defaults

Download a Docker container

sudo docker pull ubuntu

Run Docker container

Setup a basic ubuntu container with a bash shell, we just run one command.  docker run will run a command in a new container, -i attaches stdin and stdout, -t allocates a tty, -h allocates a hostname, and we’re using the standard ubuntu container.

sudo docker run -i -t ubuntu /bin/bash

That’s it! You’re now using a bash shell inside of a ubuntu docker container.

To disconnect, or detach, from the shell/container without exiting/shutting down use the escape sequence Ctrl-p + Ctrl-q.

To shutdown the container, run exit, but remember that container storage is ephemeral, meaning data isn’t saved. You’ll need to use docker volumes.

To search for other community docker containers

sudo docker search aws
sudo docker search google

List containers

sudo docker ps
sudo docker ps -a
sudo docker ps -l
sudo docker ps -a --filter "status=running"

docker ps will list running container, -a list all containers, -l list latest created containers including non-running containers, a –filter “status=running” list all running containers

Attach to a container

sudo docker attach <container-id>
sudo docker attach d8894b58ecb6

Committing changes to your container

sudo docker commit <container-id> <new-container-name>
sudo docker commit d8894b58ecb6 new-docker-v1

To create a new image from changes to a container, it’s a simple as running just one command, docker commit.

View container logs

sudo docker logs <container-id>
sudo docker logs d8894b58ecb6

To view the logs for a container it’s as simple as running just one command, docker logs. If you don’t have any logs listed then no changes have been made to the container.

Restart a running container 

sudo docker stop <container-id>
sudo docker stop d8894b58ecb6
sudo docker stop d8894b58ecb6

To restart a running container, it’s a simple as running just one command, docker restart.

Stop a running container (or multiple containers)

sudo docker stop <container-id>
sudo docker stop d8894b58ecb6
sudo docker stop d8894b58ecb6 bafb4b2fc70f

To stop a running container, it’s a simple as running just one command, docker stop.

List all images/containers on this server

sudo docker images


To view docker usage stats – CPU, MEM & Network

sudo docker stats <container-id>
sudo docker stats d8894b58ecb6

Keeping persistent data – docker storage volumes

There’s no way to directly create a “data volume” in Docker, so instead we create a data volume container with a volume attached to it. For any other containers that you then want to connect to this data volume container, use the Docker’s --volumes-from option to grab the volume from this container and apply them to the current container.

sudo docker create -v /data1 --name datacontainer ubuntu

This created a container named datacontainer based off of the ubuntu image and in the directory /data1.

Now, if we run a new Ubuntu container with the --volumes-from flag anything we write to the /data1 directory will get saved

docker run -t -i --volumes-from datacontainer ubuntu /bin/bash
cd /data1
touch thisisapersistentfile.txt

The file ‘thisisapersistentfile.txt’ we created will now be persistent on a container named datacontainer and in the directory /data1.

Cleaning up docker

After starting and stop all of those containers, stuuff get left behind (so you might inspect it later if you desire). But, if you are running out of disk space because of docker, give this script a try.

# hosted at

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1

echo " current disk space"
df -h

# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v

# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi

# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
  docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr

echo "new disk space"
df -h

Docker help

sudo docker –help




How to display a static Google Map


If you have ever needed a static Google Maps image, here is the URL to use:

The bold values can be changed to suit your requirements.,lonsdale,st,melbourne,%20Australia&zoom=15&size=800×600&format=gif&sensor=false

Handy if you need a simple version to print and take with you.

You can do much more than described above like add markers and routes, just visit the Google Developers site




Install GUI & VNC on Ubuntu

Pretty simple.  You want to add a visual interface to your Ubuntu server.

Firstly, update the APT package manager

sudo apt-get update

Install a minimal Unity desktop

sudo apt-get install --no-install-recommends ubuntu-desktop -y

GUI should now be installed, reboot to take effect.

sudo reboot

This is what is looks like upon reboot (after login)

To get to a TTY terminal.
For Text Mode
for GUI Mode



Add TightVNC
sudo apt-get install tightvncserver -y
vncserver   #set a secure password

#connect to machine on VNC requires a VNC client




Installing Webmin on ubuntu

To install Webmin via apt-get you will have to add it to your repository:

sudo nano /etc/apt/sources.list

Go to the end and add this to the file:

deb sarge contrib
deb sarge contrib

Save the file.  Ctrl-X, Yes, Enter.

After performing the above actions, you need to get the GPG key the repository is signed with. So for that type in the following commands:

cd /root
apt-key add jcameron-key.asc

After adding the GPG key you need to update the source.list:

sudo apt-get update

Now you can install Webmin by typing:

sudo apt-get install webmin

Or alternatively you can try downloading the actual .deb file:


This will download the .deb file on to the system, then you should:

dpkg --install webmin_1.750_all.deb

This will install Webmin on your system.
Login to Webmin
Open up the browser and type in you IP address with as following:


You will get a warning for the SSL certificate, you may just proceed ignoring that warning. After you have navigated to the above URL you will get the following login page:

Log in with root user name and password.

Simple boto.cfg example file

This file can be place in either:

/etc/boto.cfg for system wide

$HOME/.boto for user specific



cat /etc/boto.cfg
# boto.cfg
# python boto config file - system wide settings

# <AWS account name>
aws_access_key_id = <access_key_id>
aws_secret_access_key = <secret_key_id>

#debug = 0
#num_retries = 10
#proxy =
#proxy_port = 8080
#proxy_user = foo
#proxy_pass = bar