david/scripts-archive
david
/
scripts-archive
Archived
1
0
Fork 0
This repository has been archived on 2022-04-16. You can view files and clone it, but cannot push or open issues or pull requests.
scripts-archive/bandwidth.sh

145 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# script to test bandwidth to ssh server
# it copies a 10MB file to the server and
# loads it back down again.
# usage: bandwidth user@server
# if no server is applied a default server is used
# defaults
sshserver=birdofprey
sshuser=david
sshpath="/tmp"
localpath="/tmp"
file=$RANDOM
size=10 #MB
silent=1
# config
if [ $# -ne 0 ]; then
sshuser=$(echo $1 | sed 's/@.*//')
sshserver=$(echo $1 | sed 's/.*@//')
if [ $# -gt 1 ]; then
echo "too much arguments. exiting."
exit 1
fi
if [ $silent -ne 1 ]; then
echo "using custom server. ($sshuser@$sshserver)"
fi
else
if [ $silent -ne 1 ]; then
echo "using default server. ($sshuser@$sshserver)"
fi
fi
# preflight
command -v scp &> /dev/null
if [[ $? -ne 0 ]]; then
echo "scp must be installed first. exiting."
exit 1
fi
command -v ssh &> /dev/null
if [[ $? -ne 0 ]]; then
echo "ssh must be installed first. exiting."
exit 1
fi
command -v python &> /dev/null
if [[ $? -ne 0 ]]; then
echo "python must be installed first. exiting."
exit 1
fi
# create file
if [ $silent -ne 1 ]; then
echo "creating $size MB file."
fi
dd if=/dev/zero of=$localpath/$file bs=$(($size*1024*1024)) count=1 &> /dev/null
if [[ $? -ne 0 ]]; then
echo "cannot create file. ($localpath/$file)"
exit 1
fi
# upload file
if [ $silent -ne 1 ]; then
echo "uploading file to $sshserver"
fi
scp -v $localpath/$file $sshuser@$sshserver:$sshpath/$file &> $localpath/bandwidth_scplogup
if [ $? -ne 0 ]; then
echo "error while uploading file. exiting."
echo "there might be information in the scp log. ($localpath/bandwidth_scplogup)"
# remove local file
rm $localpath/$file
if [[ $? -ne 0 ]]; then
echo "cannot delete local file. exiting. ($localpath/$file) - you must fix this!!"
else
if [ $silent -ne 1 ]; then
echo "local file has been deleted."
fi
fi
exit 1
fi
result=$(cat $localpath/bandwidth_scplogup | grep "Bytes per second" | awk '{print $5}' | sed 's/\,//')
result=$(echo $result | sed 's/\..*//')
echo "UPLOAD: $(($result * 8 / 1024)) kbit/s"
rm $localpath/bandwidth_scplogup
# download file
if [ $silent -ne 1 ]; then
echo "downloading file from $sshserver."
fi
scp -v $sshuser@$sshserver:$sshpath/$file $localpath/$file &> $localpath/bandwidth_scplogdown
if [ $? -ne 0 ]; then
echo "error while downloading file. exiting."
echo "there might be information in the scp log. ($localpath/bandwidth_scplogdown)"
# remove remote file
ssh $sshuser@$sshserver rm $sshpath/$file &> /dev/null
if [[ $? -ne 0 ]]; then
echo "cannot remove remote file. ($sshpath/$file) - you must fix this!!"
sleep 5
else
if [ $silent -ne 1 ]; then
echo "remote file removed."
fi
fi
exit 1
fi
result=$(cat $localpath/bandwidth_scplogdown | grep "Bytes per second" | awk '{print $7}' | sed 's/\,//')
result=$(echo $result | sed 's/\..*//')
echo "DOWNLOAD: $(($result * 8 / 1024)) kbit/s"
rm $localpath/bandwidth_scplogdown
# remove remote file
ssh $sshuser@$sshserver rm $sshpath/$file &> /dev/null
if [[ $? -ne 0 ]]; then
echo "cannot remove remote file. ($sshpath/$file) - you must fix this!!"
sleep 5
else
if [ $silent -ne 1 ]; then
echo "remote file removed."
fi
fi
# remove local file
rm $localpath/$file
if [[ $? -ne 0 ]]; then
echo "cannot delete local file. exiting. ($localpath/$file) - you must fix this!!"
exit 1
else
if [ $silent -ne 1 ]; then
echo "local file has been deleted."
fi
exit 0
fi
if [ $silent -ne 1 ]; then
echo "all done. exiting."
fi
exit 0