Skip to content

Instantly share code, notes, and snippets.

@purefan purefan/mypinger.sh
Created Jan 26, 2016

Embed
What would you like to do?
Simple ping wrapper
#!/bin/bash
DOCURL=true;
echo $0
runCurl() {
PING=$(curl --connect-timeout 1 --max-time 1 --head -s ${1});
ISUP=false;
if [[ $PING == *"200 OK"* ]]; then
ISUP=true;
fi
}
runPing() {
PING=$(ping -c 1 -t 2 $1 | grep "% packet");
if [[ $PING == *" 0.0% packet"* ]]; then
ISUP=true;
fi
}
while [ true ]; do
PREV=$ISUP;
if [ "${DOCURL}" = true ]; then
runCurl $1;
sleep 2;
else
runPing $1;
fi
WHEN=$(date);
if [ "${ISUP}" = true ]; then
if [ "${PREV}" = $ISUP ]; then
echo -n ".";
else
echo "${WHEN} ${1} Its all good";
fi
else
echo "${WHEN} ${1} is down!!";
LOG="/var/log/${1}.downtime.log"
echo -e "${WHEN} Failed:\n\t${PING}" >> $LOG
tput bel;
sleep 1;
tput bel;
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.