Skip to content

Instantly share code, notes, and snippets.

@zxjinn
Last active August 29, 2015 14:09
Show Gist options
  • Save zxjinn/e586df380a2912f48eae to your computer and use it in GitHub Desktop.
Save zxjinn/e586df380a2912f48eae to your computer and use it in GitHub Desktop.
Line speed test
#!/bin/bash
# This is the case on Ubuntu
# TODO: Write checks for other OSes
which nc.traditional 1>&2> /dev/null
return_value=$?
if [ ${return_value} -ne 0 ]; then
echo "Traditional NC doesn't appear to be installed, please install 'netcat-traditional'"
exit 1
fi
which bc 1>2&> /dev/null
return_value=$?
if [ ${return_value} -ne 0 ]; then
echo "It appears BC is not installed, please install 'bc'"
exit 1
fi
port_number=9999
temporary_file=$(mktemp)
echo "Netcat is listening on port ${port_number}, waiting for network input..."
nc.traditional -v -v -l -n -p ${port_number} 2>&1 | grep -v ^y &> ${temporary_file}
bytes_received=$(tail -n 1 ${temporary_file} | awk {'print $NF}')
rm ${temporary_file}
echo "${bytes_received} bytes received"
echo "Enter the number of 'real' seconds from the other machine, without the trailing 's'"
echo -n "Time: "
read real_seconds
megabits_per_second=$(echo "((${bytes_received} * 8) / 1000 / 1000) / ${real_seconds}" | bc -l)
echo "${megabits_per_second:0:8} Megabits per second"
#!/bin/bash
if [ ""${1}"" == "" ]; then
echo "You've got to pass an IP address to send data to, e.g.:"
echo "${0} 192.168.1.2"
fi
port_number=9999
temporary_file=$(mktemp)
echo "Sending data.. hit CTRL+C when you're done"
/usr/bin/time --output=${temporary_file} --format="real %e" yes | nc -n ${1} ${port_number} 2>&1 > /dev/null
echo
echo
time_output=$(cat ${temporary_file} | grep -i real)
echo "Input this number on the other server: ${time_output}"
rm ${temporary_file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment