NetworkManager Dispatcher

To keep my data backed up savely I rely on CrashPlan. For the last weeks I had a quite long bug request pending. What I took from it, was the problem CrashPlan had when the network is not yet available when the service is started. Therefore I disabled its automatic startup and wrote a dispatcher script for the NetworkManager to automatically start crashplan, whenever I get a network connection and stop it, when the connection breaks down. Find the script here: 02crashplan.sh

During mmy research I found out, that the dispatcher service was not enabled on my Arch Linux system. That’s important for the scripts to work.

$ sudo systemctl enable NetworkManager-dispatcher

Similarly the service for the modemmanager was disabled, which caused my UMTS card to not work.

$ sudo systemctl enable ModemManager

While working on this, I wrote another dispatcher script, which takes care of connecting to the univeristy vpn (works for all Munich universities, which get their internet services from the lrz) automatically, when I connect to the corresponding network. Find the script here: 01lrzvpn.sh
The idea for this script and its frame is taken from Ubuntuusers.de. Make sure, to save your credentials in the corresponding VPN configuration file and update the name of the VPN in the script to the one you are using.

Bash Example Script #2

I have added the following function to my bash example script:

# Run external command and only procced, when exit code is 0
function try {
	$@
	returnValue=$?
	echo "$returnValue"
	if [ $returnValue -ne 0 ]
	then
		log $ERROR "$@ exited with return value \
		$returnValue Script will stop here."
		echo "$@ exited with return value $returnValue. \
		Script will stop here."
		exit $EXIT_FAILURE
	fi
}

The function takes a program as an argument and runs in. If the program returns with a status other then 0, so it fails, then the script will print an error message, log it and and exits. I needed this behaviour in some recent scripts and hope it to be usefull for everybody else.

Bash Example Script

These days I use bash more and more often for simple tasks on my Linux system, as well as for a University course I am taking.

Have a look at the script:

#!/bin/bash
# Skript: bash_example.sh
# Credit to: http://www.pro-linux.de/artikel/2/111/
#1,ein-shellskript-template.html
#
# I have added some usefull functions that I am using in many
# of my scripts
# Find this script here: http://phynformatik.de
# Janosch Maier
#
# This work is licensed under the
# Creative Commons Attribution-Share Alike License
#
# You are free
# to Share — to copy, distribute and transmit the work
# to Remix — to adapt the work
# to make commercial use of the work
#
# Under the following conditions:
# Attribution — You must attribute the work in the manner
# specified by the author or licensor (but not in any way
# that suggests that they endorse you or your use of the work).
# Share Alike — If you alter, transform, or build upon this
# work, you may distribute the resulting work only under the
# same or similar license to this one.
version="0.1"
lastChange="14.12.2011"

#### CONFIG - DO NOT MAKE CHANGES HERE ####
# PATH has to be a trusted value
# Try to make this script save in unsave environments
# "." cannot be in PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin
export PATH

# Environment variable IFS should always be
# "" or undefined
# Same as with PATH
if [ -z "$IFS" ]
then
        IFS="
"
        export IFS
fi

#### YOU CAN START MAKING CHANGES FROM HERE ON ####
# Define global variables for later use
# Name of the script is its filename (without possible suffix .sh)
SCRIPTNAME=$(basename $0 .sh)
# Define Exit values
EXIT_SUCCESS=0
EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_BUG=10
# Logfile will be put under current working directory
LOGFILE=`readlink -f "$SCRIPTNAME.log"`

# Error values
ERROR="ERROR"
WARNING="WARNING"
INFO="INFO"
NOTICE="NOTICE"

# Default values for options
VERBOSE=false
DEBUGLEVEL=1

#### FUNCTIONS FOR LATER USE ####
# Print usage message
function usage {
	echo "Usage: $SCRIPTNAME [-h] [-v] [-d debuglevel] file ...

  -h		show this help
  -v		be verbose
  -d		set debug level (1-9)
	[[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
}

# Log stuff to the defined logfile
# Call "log $ERROR|$WARNING|$INFO|$NOTICE message"
function log {
	case $1 in
		$ERROR)
			if [ $DEBUGLEVEL -ge 1 ]
			then
				shift
				echo "$(date '+%F %T') $ERROR: $@" \
                                >> $LOGFILE
			fi
			;;
		$WARNING)
			if [ $DEBUGLEVEL -ge 3 ]
			then
				shift
				echo "$(date '+%F %T') $WARNING: $@" \
                                >> $LOGFILE
			fi
			;;
		$INFO)
			if [ $DEBUGLEVEL -ge 5 ]
			then
				shift
				echo "$(date '+%F %T') $INFO: $@" \
                                >> $LOGFILE
			fi
			;;
		$NOTICE)
			if [ $DEBUGLEVEL -ge 7 ]
			then
				shift
				echo "$(date '+%F %T') $NOTICE: $@" \
                                >> $LOGFILE
			fi
			;;
		*)
			echo "$(date '+%F %T') BUG: $@" \
                        >> $LOGFILE
			;;
	esac
}

# Echo a string if verbose mode is set
# Log as an Info message in any case
function echo_verbose {
	if [ "$VERBOSE" == "true" ]
	then
		echo $@
	fi
	log $INFO "$@"
}

# Echo a string and put it to Error log
function echo_error {
        log $ERROR "$@"
        echo "$ERROR: $@" >&2
}

#### GETTING ALL OPTIONS #####
# Option -h gives some help
# Option -v makes the script verbose
# Option -o has one option argument therefore o:
while getopts ':o:d:vh' OPTION ; do
	case $OPTION in
		v) VERBOSE=true
		;;
 		h) usage $EXIT_SUCCESS
 		;;
		d) DEBUGLEVEL="$OPTARG"
		;;
 		\?) echo "Unknown Option \"-$OPTARG\"." >&2
 		usage $EXIT_ERROR
 		;;
		:) echo "Option \"-$OPTARG\" needs an argument." >&2
		usage $EXIT_ERROR
		;;
		*) echo "There should never happen..." >&2
		usage $EXIT_BUG
		;;
	esac
done

#### DO CHECKS ON THE ARGUMENTS ####
# $OPTIND is number of options gotten by getopts + 1
# Therefore shift arguments gotte by OPTIND - 1
shift $(( OPTIND - 1 ))

# Check if the number of arguments gotten is correct
if (( $# < 1 || $# > 2)) ; then
	echo "One or two arguments needed." >&2
	log ERR "1 or 2 arguments needed. $# arguments gotten"
	usage $EXIT_ERROR
fi

#### DO SOME STUFF HERE ####
# Do something for all other arguments
for ARG ; do
	echo_verbose "Doing stuff for an argument"
	echo $ARG
done

# The script succeded doing its work
exit $EXIT_SUCCESS

 

I have tried to comment the script as good as possible. Explanation for some parts of the script can be found at the page the base for this script originates from. Feel free to ask questions, if something is not understandable.

 

Download the script: bash_example.sh.

Use TUMonline Calendar with Google Calendar

By default it is not possible to include the calendar provided by TUMonline the system of the technical university munich into Google Calendar.

The problem is, that Google Calendar only accepts files with the suffix .ics. I wrote a script that takes a TUMonline calendar link and converts it, that it is usable by Google. If you use this link, you will be redirected to your calendar.

You can either use the version hosted by me or download the source code (licenced under the EUPL) and host the script yourself. You only need a Apache server with mod_rewrite enabled.

Here is the script: http://phynformatik.de/calendar/index.php

And: If you think it is usefull. You have the possibility to flattr it again!