#!/bin/bash # Usage: doPingTest [] function doPingTest { HOST=${1} logScript "Doing ping to ${HOST}" COUNT=1 [ "${2}" = "" ] || COUNT=${2} ping "${HOST}" -W 10 -c ${COUNT} | { read -r HEADER RESOLVED_NAME=$(echo ${HEADER} | sed 's/.* (\(.*\)) .*/\1/') while read -r PINGLINE; do if [[ "${PINGLINE}" == *" bytes from "* ]]; then SUCCESS=true BYTES=$(echo ${PINGLINE} | awk '{ print $1 }') TTL=$(echo ${PINGLINE} | sed 's/.*ttl=\([0-9]\+\) .*/\1/') LATENCY=$(echo ${PINGLINE} | sed 's/.*time=\([0-9\.]\+\) .*/\1/') elif [ "${PINGLINE}" = "" ]; then SUCCESS=false BYTES=0 TTL=0 LATENCY=-1 else continue fi writeData \ ping \ "sourceHost=$(hostname -f),targetHost=${HOST}" \ "success=${SUCCESS},resolved=\"${RESOLVED_NAME}\",latency=${LATENCY},ttl=${TTL},bytes=${BYTES}" done } }