Bash Scripts

De WikiMar
Dreceres ràpides: navegació, cerca

Update external DNS

#!/bin/bash
# Put a symbolic link to this script in  /etc/network/if-up.d/ so that it runs on network change
# Also put it in the crontab -e

NAMEDNS="portatilelite"
SSHKEY="/home/marti/.ssh/id_dsa"

LOGFILE="${0}.log"
LOGFILE="/home/marti/$(basename $LOGFILE)" #In case the script is run from a symbolic link (ex. /etc/network/if-up.d/), we force another specific folder for the logs

ssh -p23 -o ConnectTimeout=2 -o ConnectionAttempts=1 -i $SSHKEY marti@$NAMEDNS.ip.min.com exit
# > /dev/null 2>&1

if [ $? -ne 0 ] ; then
	# echo "$DATE - Cannot connect to $NAMEDNS.ip.min.com" |tee -a $LOGFILE

	wget -q --output-document=/tmp/$NAMEDNS.ip.min.com.txt "http://ip.min.com/update.php?ps=test&domain=$NAMEDNS" &
	PID=$!
	sleep 10
	if [ "$(ps ax | grep $PID)" != "" ]; then
		# kill $PID
		kill -s SIGKILL "$PID"
		echo "$(date +'%Y-%m-%d %H:%M:%S') - Cannot connect to ip.min.com" |tee -a $LOGFILE
   	else
		echo "$(date +'%Y-%m-%d %H:%M:%S') - New IP for $NAMEDNS.ip.min.com $(head -n 1 /tmp/$NAMEDNS.ip.min.com.txt)" |tee -a $LOGFILE
	fi
fi