#! /bin/sh # # wonderping.sh - find the highest upstream with acceptable latency # Copyright (C) 2006 Tommi Saviranta # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # Version: wonderping.sh v0.1.1 22-Jan-2006 wnd@iki.fi WANT=50 # desired latency UP=600 # initial upstream (kb/s) DOWN=7000 # downstream (kb/s) STEP=100 # initial increment for upstream PING="83.145.228.190" # host to ping COUNT=128 # ping N times IFACE="eth1" # interface to tweak FILE="/tmp/bob" # scp $FILE $TARGET TARGET="user@host.com:" # scp $FILE $TARGET RESULT="log" # result file TMP=$(tempfile) trap "test -e "$TMP" && rm \"$TMP\"; exit" 0 1 2 15 test -e "$RESULT" && rm "$RESULT" do_ping() { IP="$1" ping -c $COUNT $PING 2>&1 | tee "$TMP" LOSS=$(tail -n 2 "$TMP" | head -n 1 | sed 's/.* \([0-9]*\)%.*/\1/') LINE=$(tail -n 1 "$TMP" | sed 's/.*= \(.*\) m.*/\1/') MIN=$(echo "$LINE" | cut -d '/' -f 1 | cut -d '.' -f 1) AVG=$(echo "$LINE" | cut -d '/' -f 2 | cut -d '.' -f 1) MAX=$(echo "$LINE" | cut -d '/' -f 3 | cut -d '.' -f 1) MDEV=$(echo "$LINE" | cut -d '/' -f 4 | cut -d '.' -f 1) } transfer() { scp -q "$FILE" "$TARGET" & PID=$! } while true; do echo "Upstream: $UP kb/s" wondershaper clear "$IFACE" wondershaper "$IFACE" $DOWN $UP transfer "$FILE" "$WANT" sleep 5 do_ping kill $PID echo "$UP\t$MIN\t$AVG\t$MAX\t$MDEV\t$LOSS" >>"$RESULT" if test $AVG -lt $WANT; then UP=$(expr $UP + $STEP) GOTUP=1 elif test $AVG -gt $WANT; then if test $GOTUP -eq 1; then STEP=$(expr $STEP / 2) test $STEP -eq 0 && break fi UP=$(expr $UP - $STEP) fi done sort -n "$RESULT" >"$TMP" mv "$TMP" "$RESULT" echo "All done. See \"$RESULT\" for results (use GnuPlot)."