#!/bin/bash
#
# recordum_ip_ping
#
# v0.1 2017-11-03 RE
#
# ping host to test connectivity, name resolution and system interfaces

MYSELF=$( basename $0 )
TIMESTAMP=$( date +%Y%m%d-%H%M%S )

# helper
script_help() {
    cat <<EOF
${MYSELF} help:

This script pings a host.

Usage: ${MYSELF} -h <host> -s <source>

where:
    <host>   ... numeric IP (v4) or FQDN (to test name resolution)
    <source> ... May be numeric IP or name of device eth0|eth1|tun0|ppp0|lo

EOF
}

if [[ $# -lt 2 ]]
then
    script_help
    exit 1
fi

while getopts "h:s:" option
do
    case ${option} in
        h) HOST=$OPTARG
            ;;
        s) SOURCE=$OPTARG
            ;;
        *) script_help
            exit 2
            ;;
    esac
done

# set defaults
if [[ -z "$HOST" ]]
then
    HOST="portal-eu.recordum.net"
fi
if [[ -z "$SOURCE" ]]
then
    SOURCE="eth0"
fi
# set internal options
# maximum number of pings
MAX_PING=6
# wait time
WAIT=9

# submit the ping command
ping -c ${MAX_PING} -w ${WAIT} -I ${SOURCE} ${HOST}
# full success
exit 0
