Useful shell scripts

Prevent Idle Shutdown

This script will prevent kodi from auto powering off.

It uses a nice feature call InhibitIdleShutdown and the kodi-send tool.

Every 60 seconds the script will check of any active SSH, SAMBA or NFS connections. As long as these is one ore more of those connections it will remain switched on. This should be added into a separate file and called from autostart.sh This will continue to monitor the state of any active connections forever.

When there are no connects the IdleShutdownInhibit function will be re-enabled. This will not shutdown the server until it has been idle for the time you have specified in kodi.

#/bin/sh

IDLE_SHUTDOWN_ALLOWED_LAST_STATE=-1

while true
do
	KODI_RUNNING=`ps -A | grep kodi.bin | grep -v grep | wc -l`

	if [ 1 == $KODI_RUNNING ] ; then
		SSH_ACTIVE=`netstat -tnpa | grep 'tcp.*:22.*ESTABLISHED.*' | wc -l`
		NFS_ACTIVE=`netstat -tnpa | grep 'tcp.*:111.*ESTABLISHED.*' | wc -l`
		SMB_ACTIVE=`netstat -tnpa | grep 'tcp.*:445.*ESTABLISHED.*' | wc -l`  
		[ $SSH_ACTIVE -gt 0 -o $NFS_ACTIVE -gt 0 -o $SMB_ACTIVE -gt 0 ] && IDLE_SHUTDOWN_ALLOWED=1 || IDLE_SHUTDOWN_ALLOWED=0 

		if [ $IDLE_SHUTDOWN_ALLOWED_LAST_STATE != $IDLE_SHUTDOWN_ALLOWED ] ; then
			IDLE_SHUTDOWN_ALLOWED_LAST_STATE=$IDLE_SHUTDOWN_ALLOWED
			kodi-send --action="AllowIdleShutdown"
			if [ 0 == $IDLE_SHUTDOWN_ALLOWED ] ; then
				kodi-send --action="InhibitIdleShutdown(false)"
			else
				kodi-send --action="InhibitIdleShutdown(true)"
			fi
		fi
	fi
	sleep 60
done


DYNU Dynamic IP Updater

This script will check the public IP address of the network gateway. It will then submit this to dynu.com. This allows you to allocate a free domain name to your network and keep it up to date.

Again this script should be run in autostart.sh.

WARNING: Some care should be taken not to set the refresh rate too low (< 60 seconds) otherwise the servers that report back your public IP will block your requests.

#!/bin/sh

# Modify These
DYNU_USERNAME="username"
DYNU_PASSWORD="password-hash"
DYNU_HOSTNAME="example.dynu.com"
REFRESH_RATE=60

# Leave set to 0.0.0.0
PUBLIC_IP=0.0.0.0

test_ip ()
{
	if [ -n $2 ] ; then
		if [ $1 != $2 ] ; then
        		update_ip $2
			echo $2
		else 
			echo $1
		fi
	fi
}

update_ip ()
{
	DYNU_WGET="https://api.dynu.com/nic/update?hostname=$DYNU_HOSTNAME&myip=$1&username=$DYNU_USERNAME&password=$DYNU_PASSWORD"
	DYNU_UPDATE=`wget $DYNU_WGET -O - -q ; echo`
	(>&2 echo "$DYNU_UPDATE")
}

while true; do
        PUBLIC_IP_TEST=`wget http://ipecho.net/plain -O - -q ; echo`
	echo $PUBLIC_IP - $PUBLIC_IP_TEST
        PUBLIC_IP="$(test_ip $PUBLIC_IP $PUBLIC_IP_TEST)"
        sleep $(REFRESH_RATE)
        PUBLIC_IP_TEST=`curl -s checkip.dyndns.org | sed 's#.*Address: \(.*\)</b.*#\1#'`
	echo $PUBLIC_IP - $PUBLIC_IP_TEST
        sleep $(REFRESH_RATE)
        PUBLIC_IP_TEST=`curl -s ifconfig.co`
	echo $PUBLIC_IP - $PUBLIC_IP_TEST
        PUBLIC_IP="$(test_ip $PUBLIC_IP $PUBLIC_IP_TEST)"
        sleep $(REFRESH_RATE)
done