Skip to content

Instantly share code, notes, and snippets.

@mohanraj-r
Last active March 5, 2024 10:31
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mohanraj-r/a9d4abe90201e5b4203d6cc723261ba0 to your computer and use it in GitHub Desktop.
Save mohanraj-r/a9d4abe90201e5b4203d6cc723261ba0 to your computer and use it in GitHub Desktop.
[speed test] Test ssh connection speed
#!/bin/bash
# scp-speed-test.sh
# Author: Alec Jacobson alecjacobsonATgmailDOTcom
# http://www.alecjacobson.com/weblog/?p=635
#
# Test ssh connection speed by uploading and then downloading a 10000kB test
# file (optionally user-specified size)
#
# Usage:
# ./scp-speed-test.sh user@hostname [test file size in kBs]
#
ssh_server=$1
test_file=".scp-test-file"
# Optional: user specified test file size in kBs
if test -z "$2"
then
# default size is 10kB ~ 10mB
test_size="10000"
else
test_size=$2
fi
# generate a 10000kB file of all zeros
echo "Generating $test_size kB test file..."
`dd if=/dev/zero of=$test_file bs=$(echo "$test_size*1024" | bc) \
count=1 &> /dev/null`
# upload test
echo "Testing upload to $ssh_server..."
up_speed=`scp -v $test_file $ssh_server:$test_file 2>&1 | \
grep "Bytes per second" | \
sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\1/g"`
up_speed=`echo "($up_speed*0.0009765625*100.0+0.5)/1*0.01" | bc`
# download test
echo "Testing download from $ssh_server..."
down_speed=`scp -v $ssh_server:$test_file $test_file 2>&1 | \
grep "Bytes per second" | \
sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\2/g"`
down_speed=`echo "($down_speed*0.0009765625*100.0+0.5)/1*0.01" | bc`
# clean up
echo "Removing test file on $ssh_server..."
`ssh $ssh_server "rm $test_file"`
echo "Removing test file locally..."
`rm $test_file`
# print result
echo ""
echo "Upload speed: $up_speed kB/s"
echo "Download speed: $down_speed kB/s"
@kuasha420
Copy link

Nice script! Although I wonder, what the heck is happening here:

($up_speed0.0009765625100.0+0.5)/1*0.01"

@bvbanks
Copy link

bvbanks commented Apr 1, 2019

Nice script! Although I wonder, what the heck is happening here:

($up_speed_0.0009765625_100.0+0.5)/1*0.01"

That's a conversion from kibibyte to kilobyte. 1/1.024 is 0.9765625.

@vsmelov
Copy link

vsmelov commented Jan 9, 2020

maybe better to replace

dd if=/dev/zero of=$test_file bs=$(echo "$test_size*1024" | bc) \

with

dd if=/dev/random of=$test_file bs=$(echo "$test_size*1024" | bc) \

because if compression is used, all zeros will significantly speed up the connection.

@vitalz
Copy link

vitalz commented May 1, 2020

A good catch on randoms instead of zeros!

@TheAmanTiwari
Copy link

How to specify port and SSH key?

@bheadmaster
Copy link

Random note:

Use /dev/urandom for generating random data. /dev/random blocks if there isn't enough entropy in the system (try and do cat /dev/urandom and see for yourself - and try moving your mouse randomly after it blocks). /dev/urandom is a pseudo-random number generator, so as long as it's seeded it can generate a virtually unlimited amount of random data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment