I’m trying to create a simple script to check if a VRRP router able to ping at least one IP/router before it considered as failed. However, the script doesn’t properly captured the correct number and values of passed arguments. I believe it relates to these lines here:
if [ "$(id -g -n)" != 'vyattacfg' ] ; then
exec sg vyattacfg -c "/bin/vbash $(readlink -f $0) $@"
fi
Full Script:
#!/bin/vbash
echo "--------------------------------------------";
echo "Before vyattacfg";
echo "Number of address passed: ${#}";
echo $@;
echo "--------------------------------------------";
if [ "$(id -g -n)" != 'vyattacfg' ] ; then
exec sg vyattacfg -c "/bin/vbash $(readlink -f $0) $@"
fi
# Requires at least 1 router loopback address (IPv4/IPv6).
# The script will exit with 0 (success) even though no router address is specified. So, be sure to correctly configure this.
# The best way to test this is to run the script and look at the exit code before applying it to VRRP.
# I don't bother checking whether the formatting of the address is correct or not.
# Check is considered failed if ping to ALL specified routers failed. If even 1 ping successful, the check considered a success.
NUM_OF_ARGS=$#;
ALL_ARGS=$@;
echo "--------------------------------------------";
echo "After vyattacfg";
echo "Number of address passed: ${NUM_OF_ARGS}";
echo $ALL_ARGS;
echo "--------------------------------------------";
ADD_TO_LOG () {
echo "[$(date +%d/%m/%Y) $(date +%H:%M:%S)] ${1}" >> /var/log/scripts/vrrp-check.log
}
source /opt/vyatta/etc/functions/script-template;
if [ $NUM_OF_ARGS -lt 1 ]
then
echo "Usage: $0 <RTR1_LOOPBACK_IP> <RTR2_LOOPBACK_IP> ...";
echo "You need to specify at least 1 router IPv4/IPv6 address!";
exit 0;
else
RESULT_ARR=();
for ARG in $ALL_ARGS
do
ADD_TO_LOG "Running ping to $ARG";
/bin/ping -c 1 -i 1 $ARG;
PING_RESULT=$?;
ADD_TO_LOG "Ping to ${ARG}, result code: ${PING_RESULT}";
done
fi
Test command:
sudo /config/scripts/vrrp-check.sh 10.0.5.32 10.0.5.33 10.0.5.34 10.0.5.35
Output:
--------------------------------------------
Before vyattacfg
Number of address passed: 4
10.0.5.32 10.0.5.33 10.0.5.34 10.0.5.35
--------------------------------------------
--------------------------------------------
Before vyattacfg
Number of address passed: 1
10.0.5.32
--------------------------------------------
--------------------------------------------
After vyattacfg
Number of address passed: 1
10.0.5.32
--------------------------------------------
Any idea how? Thank you