#!/bin/sh exit1() { echo $@ >&2 exit 1 } err() { echo $@ >&2 exit_status=1 } get_default_gw() { echo $(route | grep default | awk '{ print $2 }') } test_eth_device() { local readonly dev=$1 local readonly ethernet_ping_ipaddr=$2 local readonly MINDATASIZE=56 local readonly MAXDATASIZE=650 local readonly STEPSIZE=100 local readonly ITERATION=2 local datasize local i local ping_err for i in `seq 1 $ITERATION`; do datasize=$MINDATASIZE while [ $datasize -le $MAXDATASIZE ]; do ping -I $dev -c 1 -s $datasize $ethernet_ping_ipaddr > $STATISTICS ping_err=$? packets_received=`cat $STATISTICS | grep "received" | awk '{print$4}'` # Evaluate possible errors on the ping operation if [ $ping_err -ne 0 ] || [ -z "$packets_received" ] || [ $packets_received -eq 0 ]; then err "FAIL: ping $ethernet_ping_ipaddr through $dev" cat $STATISTICS >> $LOGFILE echo "Size: $datasize Iteration: $i" >> $LOGFILE fi datasize=$((datasize+STEPSIZE)) done done } clean_tasks() { echo "Executing clean up tasks" rm -f $LOGFILE $STATISTICS } cleanup() { echo "Aborting script execution" clean_tasks exit 1 } readonly LOGFILE=`/bin/mktemp` readonly STATISTICS=`/bin/mktemp` exit_status=0 trap cleanup SIGHUP SIGINT SIGTERM readonly DEF_GW=$(get_default_gw) [ -n "$DEF_GW" ] && echo "Found default gw $DEF_GW" || exit1 "FAIL: no default gw" readonly S=: readonly DEVS="$(route -n | tail -n +3 | sort -k8 -u | awk '{print $8"'$S'"$2}')" if [ -n "$DEVS" ]; then echo "Will test: $DEVS" else exit1 "FAIL: no ethernet devices to test" fi for iface_gw in $DEVS; do iface=$(echo "$iface_gw" | sed -n 's/\(.*\)'$S'.*/\1/p') gw=$(echo "$iface_gw" | sed -n 's/.*'$S'\(.*\)/\1/p') if [ -z "$gw" ]; then err "FAIL: no ethernet gateway for $iface" else [ "$gw" = 0.0.0.0 ] && gw="$DEF_GW" echo "Testing $iface:$gw" test_eth_device $iface $gw fi done # Report failures if [ $exit_status -ne 0 ]; then echo "=================== error report ===================" cat $LOGFILE echo "====================================================" echo "FAIL: ping test" else echo "PASS: ping test success" fi clean_tasks exit $exit_status