-
Notifications
You must be signed in to change notification settings - Fork 3
/
corrping.sh
executable file
·102 lines (94 loc) · 2.76 KB
/
corrping.sh
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
# Testing how well ping and curl latency correlates.
# So far, the relationship is linear, following curl = ping*1.0366 - 6.5
# awkt 'BEGIN {tol=15; a1=-6.5; b1=1.0366; a2=4; b2=1}
# {total++; y1=$1*b1+a1; y2=$1*b2+a2}
# (y1 < $2+tol && y1 > $2-tol) || (y2 < $2+tol && y2 > $2-tol) {pass++}
# END {print pass, total, 100*pass/total}
# ' curlping.log
DATA_DIR="$HOME/.local/share/nbsdata"
SILENCE="$DATA_DIR/SILENCE"
ASN_CACHE="$DATA_DIR/asn-cache.tsv"
SLEEPTIME=5 #seconds
SERVER='www.gstatic.com'
function main {
# Get script directory.
if readlink -f dummy >/dev/null 2>/dev/null; then
scriptdir=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
else
echo "Error: readlink command required." >&2
exit 1
fi
echo -n > ~/tmp/ping.txt
echo -n > ~/tmp/curl.txt
echo -n > ~/tmp/httplib.txt
lan_ip=''
while true; do
if [[ -e "$SILENCE" ]]; then
sleep "$SLEEPTIME"
continue
fi
# Get info about current connection.
interface=$(ip route show | sed -En 's/^default .* dev ([a-zA-Z0-9:_-]+) .*$/\1/p')
lan_ip_current=$(dev_ip $interface)
if [[ $lan_ip != $lan_ip_current ]]; then
lan_ip="$lan_ip_current"
asn=$(get_asn "$lan_ip")
fi
# Collect data from last pings.
ping_time=$(cat ~/tmp/ping.txt)
curl_time=$(cat ~/tmp/curl.txt)
httplib_time=$(cat ~/tmp/httplib.txt)
if [[ $ping_time ]] && [[ $curl_time ]] && [[ $httplib_time ]]; then
echo -e "$ping_time\t$curl_time\t$httplib_time\t$asn\t$interface"
fi
# Perform the pings.
echo -n > ~/tmp/ping.txt
echo -n > ~/tmp/curl.txt
echo -n > ~/tmp/httplib.txt
ping_wrap $SERVER > ~/tmp/ping.txt &
curl_wrap $SERVER > ~/tmp/curl.txt &
$scriptdir/httplib-ping.py > ~/tmp/httplib.txt &
# Sleep.
sleep $SLEEPTIME
done
}
function ping_wrap {
server="$1"
ping -n -c 1 $server | sed -En 's/^.* bytes from .* time=([0-9.]+) ?ms.*$/\1/p'
}
function curl_wrap {
server="$1"
sec=$(curl -s --write-out '%{time_connect}\n' --output /dev/null $server)
echo "$sec*1000" | bc
}
function dev_ip {
query="$1"
last=""
ifconfig | while read line; do
if [[ ! "$last" ]]; then
dev=$(echo "$line" | sed -r 's/^(\S+)\s+.*$/\1/g')
fi
if [[ $dev != $query ]]; then
continue
fi
if [[ "$line" =~ 'inet addr' ]]; then
echo "$line" | sed -r 's/^\s*inet addr:\s*([0-9.]+)\s+.*$/\1/g'
fi
last=$line
done
}
function get_asn {
wan_ip=$(curl -s ipv4.icanhazip.com)
if [[ $wan_ip ]]; then
asn=$(awk -F '\t' '$1 == "'$wan_ip'" {print $2}' $ASN_CACHE | head -n 1)
if [[ ! $asn ]]; then
asn=$(curl -s http://ipinfo.io/$wan_ip/org | grep -Eo '^AS[0-9]+')
if [[ $asn ]]; then
echo -e "$wan_ip\t$asn" >> $ASN_CACHE
fi
fi
echo $asn
fi
}
main "$@"