aboutsummaryrefslogtreecommitdiff
blob: d1ff1c73fd78e11aa44c08056b96037cc7ae7d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/bin/queryhost.sh,v 1.6 2004/10/04 13:56:50 vapier Exp $

# Ping a single host and collect the round-trip time.  Unfortunately
# this measures latency, not bandwidth, but it's better than nothing.
pinghost() {
    local host result

    # Extract the hostname from the URL
    host="${1#*://}"; host="${host%%/*}"

    # Attempt to ping the host three times, with an overall timeout of
    # 10 seconds.
    result=`ping -q -c3 -w10 ${host} 2>/dev/null`

    # Extract average ping time, truncated to integer
    result="${result%.?/*}"
    result="${result##*/}"

    # Test for sensible $result and return.  If zero packets were
    # received, then $result will not be sensible since the above
    # extraction would have failed.
    if [ "$result" -gt 0 ] 2>/dev/null; then
	return $result
    else
	return 9999
    fi
}

# Ping all of the hosts in parallel, collate the output.
pingall() {
    local i output

    for i in $*
    do
	# Do this as a single echo so it happens as a single
	# "write".  This is so that the writes coming from the
	# multiple processes aren't mixed within a line.  It should
	# usually work.  :-)
	( pinghost $i; echo "$? $i" ) &
    done
    wait
}

pingall $1 | sort -n | awk '{print $NF}'