Hey,
i wrote a script to create a 3 IP failover security for a new project in my company.
It is not coded that suggestive up to now, its just that i need it to work in a short perfiod of time.
My problem is the following function:
Code:
function getgate () {
count=$(ping -c $COUNT $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
return -1
else
return 0
fi
}
It often happens that the ping reply is too slow for the script, so i receive an -eq unary operator expected error.
This happens because the grep function is active before the ping reply is sent to the client (=the script).
I tried to add a sleep x, but than the reply doesn't "arrive" at all.
Any ideas of how to delay the script to listen for a ping reply for like 2-3 seconds?
Thanks!
John
Here is the whole script, just if you guys want to take a look what it does, exactly.
Code:
#!/bin/bash
# IDA - A simple SHELL script for Linux and UNIX to monitor & update DYNDNS-Domains using three IP's, one primary IP and two failover IP's
# -------------------------------------------------------------------------------------------------------------
# Copyright (c) 2010 xxx v0.4
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------------------------------------------
###global settings
#Use HTTPS 1=True 0=False
HTTPS=1
#HTTPS Protocol (auto/SSLv2/SSLv3 - BE SURE WGET HAS BEEN COMPILED WITH THE CHOOSEN PROTOCOL INSTALLED, THERE IS NO DEBUG ON THIS UP TO NOW
#HTTPS Protocol BE AWARE, THIS SCRIPT WILL NEVER CHECK A CERTIFICATE FOR ITS TRUSTLEVEL
HTTPSPROTOCOL=auto
### Add ip / hostname separated by white space
# HOST_1= Primary IP
HOST_1="xx.xx.xx.xx"
# HOST_2= Secondary IP
HOST_2="xx.xx.xx.xx"
# HOST_3= Third IP
HOST_3="xx.xx.xx.xx"
### Dyndns Usersettings
DYNTOCHECK=xxx.xxx.net
#DYN's to update: seperate by "," for multiply (NO SPACE)
DYNTOUPDATE=xxx.xxx.net
#Login data
DYNUSER=xxxxx
DYNPASS=xxxxx
###Number of ping request (recommended: 1)
COUNT=1
###Interval of check (in seconds; recommend 90s)
CHECKTIME=120
### Logging (This script is writing daily logs!!!)
#logdir (FOLDER MUST EXIST)
LOGDIR="log"
#keep time in days (default: 90) // NOT WORKING YET
LOGKEEP=90
#LOGLEVEL: 101=OKs not important | _201=FAILs important | _301=OKs intresting | _401= FAILs intresting
#NOT WORKING UP TO NOW (loglevel)
#LOGLEVEL=1
###Report Email on IP failure to, seperate by "," ### ONLY IMPORTANT FAILS WILL BE SEND BY MAIL UP TO NOW (_201)
EMAILID="xxx@xxx.net"
###Planed SMS Gateway (email2sms), not active yet!
#SMSEMAILID="gateway@sms-service.com"
################## CHANGE NOTHING BELOW THIS UNTIL YOU KNOW EXACTLY WHAT YOU ARE DOING ##################
###functions
#used to check any given ip for it's availability
function getgate () {
count=$(ping -c $COUNT $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
return -1
else
return 0
fi
}
#check for ip 2 and 3 if 1 is up
function checkgate () {
count=$(ping -c $COUNT $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
dynsendmail $1
else
return 0
fi
}
#compare and if needed, set new ip!
function comparegate () {
if [ "$ACTIVEIP" == "$1" ]; then
dynoutput "101 OK - No change needed"
else
dynoutput "301 OK - Active IP does not match available higher ranked IP, changing to $1"
setgate $1
fi
}
#set new gate using input $1 as IP..
function setgate () {
if [ "$HTTPS" == "0" ]; then
result=$(wget -q -O - "http://$DYNUSER:$DYNPASS@members.dyndns.org/nic/update?hostname=$DYNTOUPDATE&myip=$1&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG")
dynreturn $result
else
result=$(wget --secure-protocol=$HTTPSPROTOCOL --no-check-certificate -q -O - "https://$DYNUSER:$DYNPASS@members.dyndns.org/nic/update?hostname=$DYNTOUPDATE&myip=$1&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG")
dynreturn $result
fi
}
#exploit DYNDNS-return result
function dynreturn () {
case $1 in
good)
dynoutput "301 OK - Updated to $2"
;;
nochg)
dynoutput "301 OK - Active IP matched new IP $2"
;;
badauth)
dynoutput "401 FAIL - The username or password specified are incorrect."
;;
!donator)
dynoutput "401 FAIL - An option available only to credited users."
;;
badagent)
dynoutput "401 FAIL - The user agent that was sent has been blocked or no user agent was speci?ed. CHECK YOUR WGET SETTINGS, IT EMULATES YOUR BROWSER"
;;
notfqdn)
dynoutput "401 FAIL - The hostname specified is not a fully-quali?ed domain name, check "DYNTOUPDATE" settings"
;;
nohost)
dynoutput "401 FAIL - The hostname specified is not known"
;;
!yours)
dynoutput "401 FAIL - The hostname specified exists, but not under the username specified."
;;
numhost)
dynoutput "401 FAIL - Too many or too few hosts found"
;;
abuse)
dynoutput "401 FAIL - The hostname specified exists, but not under the username specified."
;;
dnserr)
dynoutput "401 FAIL - DNS error encountered"
;;
911)
dynoutput "401 FAIL - There is a serious problem with the DYNDNS servers"
;;
*)
dynoutput "401 FAIL - Something bad happend, we have never seen - Error: $*"
;;
esac
}
# Active registered IP
function getactiveip () {
ACTIVEIP=$(ping -c $COUNT $DYNTOCHECK | grep 'from' | awk '{ print substr($4,1,length($4)-1) }')
sleep 3
}
###output to bash (debugging) or/and logger
function dynoutput () {
#uncommend following for debugging proposes
#echo $*
dynlogger $*
}
### LOGGING
#write logfile with date etc..
function dynlogger () {
if [ $1 == "201" ] || [ $1 == "301" ] || [ $1 == "401" ]; then
c=$(date +%Y_%m_%d)
file=$LOGDIR/idaLog_$c
d=$date
echo "$(date '+%y.%m.%d %H:%M:%S'): $*" >> $file
#if [ $1 == "201" ]; then
#need to check if status changed to 101 up to now, to provide mailspam.. hm
#mail -s "IDA // HOST Failure, check LOG: $file - TIME: $(date +%H:%M:%S) - REASON: $*" $EMAILID
#fi
fi
}
#anti mail spam test_ not working yet..
function antimailspam () {
if [ $1 == "101" ]; then
antimailspam=101
fi
if [ $1 == "201" ]; then
if [ $antispammail == "301" ]; then
return
fi
if [ $antimailspam == "201" ]; then
antispammail=301
else
antimailspam=201
fi
fi
}
#delete old logs, not implented yet..
function deloldlogs () {
return
}
### MAILING
#sendmail on failure
function dynsendmail () {
mail -s "Host $1 down" $EMAILID &> /dev/null
}
####### Start everything ########
while [ a = a ]
do
dynoutput "101 OK - DYNDNS check started.."
getactiveip
getgate $HOST_1
if [ $? -eq 0 ]; then
dynoutput "101 OK - Host 1 success"
comparegate $HOST_1
checkgate $HOST_2
checkgate $HOST_3
else
dynoutput "201 FAIL - Host 1 failed"
dynsendmail $HOST_1
getgate $HOST_2
if [ $? -eq 0 ]; then
dynoutput "101 OK - Host 2 success"
comparegate $HOST_2
checkgate $HOST_3
else
dynoutput "201 FAIL - Host 2 failed"
dynsendmail $HOST_2
getgate $HOST_3
if [ $? -eq 0 ]; then
dynoutput "101 OK - Host 3 success"
comparegate $HOST_3
else
dynoutput "201 FAIL - Host 3 failed"
dynsendmail $HOST_3
fi
fi
fi
dynoutput "101 OK - DYNDNS check stoped.."
sleep $CHECKTIME
done
exit 0