#!/bin/bash

if [[ "$#" -lt "4" ]]; then
	echo "Usage: $0 <path_to_keyfile> <remoteuser> <remotehost> <remotecommand> [regex_to_match_expected]"
	exit 3
fi

key=$1
usr=$2
host=$3
command=$4
expect=$5

command=`echo "$command" | sed 's/%%%%pipe%%%%/|/'`
##echo "###$command###"

ret=`/usr/bin/ssh -i $key ${usr}@${host} "$command" 2>&1`
stat=$?

if [[ "$stat" -eq "0" && ("$ret" =~ $expect || -z "$expect") ]]
then
	echo "OK: $ret"
else
	echo "CRITICAL: $ret"
	stat=2
fi

exit $stat
