Skip to content

Instantly share code, notes, and snippets.

@dan82840
Last active February 2, 2018 03:44
Show Gist options
  • Save dan82840/3f24620dc94f439eda6c33c3978de7e1 to your computer and use it in GitHub Desktop.
Save dan82840/3f24620dc94f439eda6c33c3978de7e1 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# ping-via-gateway.sh [Destination IP Address] [Default Gateway IP Address] [Output Interface Name]
#
SRC_IP=
DST_IP=$1
DEFAULT_GW=$2
OUT_INF=$3
PING_CNT=2
PING_TIMEOUT=5
usage() {
echo "ping-via-gateway.sh [Destination IP Address] [Default Gateway IP Address] [Output Interface Name]"
}
if [ $# -ne 3 ]; then
usage
exit 1
fi
SRC_IP=$(ifconfig $OUT_INF | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
ip route add $DST_IP via $DEFAULT_GW src $SRC_IP dev $OUT_INF
ping -I $OUT_INF -c $PING_CNT -W $PING_TIMEOUT $DST_IP
ret=$?
ip route del $DST_IP via $DEFAULT_GW src $SRC_IP dev $OUT_INF
exit $ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment