Connect to AWS EC2 VM Instance
Our first task is to connect to our EC2 VM instance via SSH. I am using the Amazon Linux AMI 2012.09.1 image. I will also be using the default ec2-user user for my scenario. If connecting from a Windows client, refer to the the Amazon AWS documentation as I will be using the Mac/Linux method with the ssh tool. I have my AWS RSA private key file stored within a hidden directory in my $HOME directory, so I will use the following command to connect to my EC2 Linux instance:
$ ssh -i ~/.aws/awskey.pem ec2-user@ec2-x-x-x-x.compute-1.amazonaws.com
Verify Python Version
The speedtest-cli tool is developed with the Python programming language. The README file states speedtest-cli is written for Python 2.4 thru 3.3. Let's verify our Python version by runnning the following command:
$ python -V
Python 2.6.8
Download and Install Git
The Amazon Linux AMI is a RPM-based distribution, so we will be using YUM for package management. Since the speedtest-cli tool is hosted at GitHub, we will make it simple by fetching the tool with Git. The git package is available from the default amzn-main repo. Run the following command:
$ sudo yum -y install git
Create Directory for Repo
We will now create the directory path to contain the speedtest-cli repo directory and then navigate into it. Run the following from the command line:
$ mkdir -p ~/repo/remote; cd $_
Clone the Repo
Let's now clone the remote repository into a newly created local directory. Run the following command:
$ git clone https://github.com/sivel/speedtest-cli.git
Copy/Install Tool
We now have a choice of where to copy the tool for scope of use. We can either elect to have the tool available for only our current user and/or system-wide. I've included examples for both.
# Current User
If we check our $PATH variable, we notice there is a reference to a bin directory within our home directory.
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin
Even though the private bin directory is defined in our $PATH variable, we still need to create it before use. Run the following commands to create the private bin directory and copy the speedtest-cli tool to it:
$ mkdir ~/bin
$ cp ~/repo/remote/speedtest-cli/speedtest_cli.py ~/bin/speedtest-cli
# System
Alternatively, we can make the tool available for all interactive users on the system. Run the following command:
$ sudo cp ~/repo/remote/speedtest-cli/speedtest_cli.py /usr/local/bin/speedtest-cli
Usage
By running the tool with no options, the speedtest-cli tool will select a Speedtest.net host nearest to your server's location to conduct the Internet connection performance test. We can change the default behavior of the tool by running it with different options.
My EC2 VM instance is physically hosted in the US East (Northern Virginia) Region, and I would like to determine what kind of network performance it will achieve when it utilizes one of the Speedtest.net hosts back here in Seattle, WA. The following command will display a list of Speedtest.net servers sorted by distance and filtered by the seattle string.
$ speedtest-cli --list | grep -i seattle
1074) Towerstream (Seattle, WA, United States) [3694.99 km]
2523) Wowrack.com (Seattle, WA, United States) [3694.99 km]
1423) Condointernet.net (Seattle, WA, United States) [3694.99 km]
2965) FiberCloud, Inc (Seattle, WA, United States) [3694.99 km]
I'm interested in the FiberCloud, Inc host for my target, so I will apply the server ID number to the server parameter value. I would also like to generate a graphical image of my test run result with the share option. Run the following command:
$ speedtest-cli --server 2965 --share# Multiple Servers
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from Amazon.com (x.x.x.x)...
Hosted by FiberCloud, Inc (Seattle, WA) [3694.99 km]: 61.671 ms
Testing download speed........................................
Download: 88.94 Mbit/s
Testing upload speed..................................................
Upload: 30.14 Mbit/s
Share results: http://www.speedtest.net/result/2583822707.png
Let's take this a step further and test our Internet connection performance against multiple servers spanning across the globe in a single batch.
For example, I would like to test against servers in Chicago, London, and Tokyo. Our first task is filter the server list to these specific locations. Run the following command:
$ speedtest-cli --list | grep -iE 'chicago|london|tokyo'
1071) Towerstream (Chicago, IL, United States) [914.11 km]
3084) SilverIP Communications (Chicago, IL, United States) [914.11 km]
1776) Comcast (Chicago, IL, United States) [914.11 km]
2574) ServerCentral (Chicago, IL, United States) [914.11 km]
958) FastSoft (Chicago, IL, United States) [915.13 km]
2153) CONNEXIONS4LONDON Ltd (Bournemouth, Great Britain) [5828.93 km]
226) Structured Communications (London, Great Britain) [5918.34 km]
385) Namesco (London, Great Britain) [5918.34 km]
3047) FidoNet (London, Great Britain) [5918.34 km]
2789) Vodafone UK (London, Great Britain) [5918.34 km]
251) World's Fastest Indian (Tokyo, Japan) [10868.84 km]
2725) Alocac, Inc. (Tokyo, Japan) [10870.93 km]
I've identified a server to test against for each location. Our next task is to insert the server ID as keys mapped to location string values (ex. [2965]="Seattle") in an associative array data type.
Note: Associative arrays were introduced in Bash version 4. Let's verify our Bash meets this requirement. Run the following command:
$ echo $BASH_VERSION
4.1.2(1)-release
We are good to go, so let's move on to creating the associative array and implementing a for loop for iteration. Run the following commands:
$ declare -A servers=( [2574]="Chicago" [2789]="London" [251]="Tokyo" )
$ for server in ${!servers[@]}; do echo "Location: ${servers[$server]}"; speedtest-cli --server $server --simple; echo; done
Location: Tokyo
Ping: 73.989 ms
Download: 31.37 Mbit/s
Upload: 3.93 Mbit/s
Location: London
Ping: 67.631 ms
Download: 68.65 Mbit/s
Upload: 22.39 Mbit/s
Location: Chicago
Ping: 64.055 ms
Download: 39.73 Mbit/s
Upload: 60.35 Mbit/s

0 comments:
Post a Comment