Skip to content

Commit

Permalink
added measure_latency.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Mayfield committed Apr 14, 2017
1 parent 6b6cd41 commit 6d09861
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -64,6 +64,27 @@ actual entropy: 84.0964047444 bits

* **chkrootkit.sh** - run chkrootkit then log & email results (chkrootkit is required)

* *measure_latency.sh* - a quick and dirty latency measurement tool

NOTE: This is just a quick tool to use so you don't have to bust out of the terminal, if you want historic views, use smokeping.

```
chad@macbookpro:~$ ./measure_latency.sh yahoo.com
ERROR: You must supply a hostname/IP to measure & a packet count!
e.g. ./measure_latency.sh <hostname/ip> <count>
chad@macbookpro:~$ ./measure_latency.sh 8.8.4.4 10
latency to 8.8.4.4 with 10 packets is: 74.735 ms
chad@macbookpro:~$ ./measure_latency.sh msn.com 10
latency measurement failed: 100.0% packet loss
-- or on linux --
ubuntu@ubuntu-xenial:~$ ./measure_latency.sh 8.8.8.8 10
latency to 8.8.8.8 with 10 packets is: 203.783 ms
ubuntu@ubuntu-xenial:~$ ./measure_latency.sh msn.com 10
latency measurement failed: 100% packet loss
```

* **randomize_mac.sh** - randomize mac addresses on macOS and Linux. This will help circumvent free wifi time limits in coffee shops and such. (This was actually an experiment until I begain using it more and more. I know about and have used machanger and spoofMAC, but I wanted to use something I wrote!)
```
macbookpro:~ $ ifconfig en0 | grep ether
Expand Down
75 changes: 75 additions & 0 deletions measure_latency.sh
@@ -0,0 +1,75 @@
#!/bin/bash

# measure_latency.sh - a dirty measure of latency via ping

# NOTE: This was only written so I could test latency quickly if I needed
# to while in the terminal (which is where I spend all my time). If
# you really want to view latency over time, use smokeping

usage() {
echo " e.g. $0 <hostname/ip> <count>"
}

if [ $# -ne 2 ]; then
echo "ERROR: You must supply a hostname/IP to measure & a packet count!"
usage
exit 1
fi

server=$1
count=$2
success=0

if [[ $OSTYPE =~ "darwin" ]]; then
# mac ping has different options that on linux
while read line
do
# check if we successfully ran
if [[ $line =~ "round-trip" ]]; then
latency=$(echo $line | awk -F "/" '/round-trip/ {print $7}')
echo "latency to $server with $count packets is: $latency"
success=1 && exit 0
fi

# check packet loss if we didn't run correctly
if [[ $line =~ "transmitted" ]] && [[ $success -ne 1 ]]; then
pct=$(echo $line | awk '/transmitted/ {print $(NF-2)}' | \
awk -F. '{print $1}')

# check packet loss
if [ $pct -eq 100 ]; then
loss=$(echo $line |awk '/tted/ {print $(NF-2),$(NF-1),$NF}')
echo "latency measurement failed: $loss"
fi
fi
done < <(ping -q -c $count -i 0.2 -t 3 $server)

elif [[ $OSTYPE =~ "linux" ]]; then
# http://homepage.smc.edu/morgan_david/cs70/assignments/ping-latency.htm
while read line
do
# check if we successfully ran
if [[ $line =~ "rtt" ]]; then
latency=$(echo $line | awk -F "/" '/rtt/ {print $5 " ms"}')
echo "latency to $server with $count packets is: $latency"
success=1 && exit 0
fi

# check packet loss if we didn't run correctly
if [[ $line =~ "transmitted" ]] && [[ $success -ne 1 ]]; then
pct=$(echo $line |awk '/transmitted/ {print $6}' | sed 's/%//g')

# check packet loss
if [ $pct -eq 100 ]; then
loss=$(echo $line |awk '/mitted/ {print $6" "$7" "$8}' | \
sed 's/,//g')
echo "latency measurement failed: $loss"
fi
fi
done < <(ping -q -c $count -i 0.2 -w 3 $server)

else
echo "ERROR: Unknown OS ($OSTYPE)"
fi

#EOF

0 comments on commit 6d09861

Please sign in to comment.