Created
July 19, 2019 08:05
-
-
Save Firxiao/babe266359724746cd4afd6420d102b2 to your computer and use it in GitHub Desktop.
Test network speed via scp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author: Firxiao | |
TEST_FILE=/root/test_file | |
LOG_FILE=/opt/speed_test.log | |
DEST_SERVER=xxxx | |
DEST_LOCATION='/tmp' | |
LOCAL_LOCATION='/tmp' | |
# Function: Setup LOG_FILE and redirect stdout/stderr. | |
log_setup() { | |
# Check if LOG_FILE exists and is writable. | |
( [ -e "$LOG_FILE" ] || touch "$LOG_FILE" ) && [ ! -w "$LOG_FILE" ] && echo "ERROR: Cannot write to $LOG_FILE. Check permissions or sudo access." && exit 1 | |
tmplog=$(tail -n $LOG_FILE_max_lines $LOG_FILE 2>/dev/null) && echo "${tmplog}" > $LOG_FILE | |
exec > >(tee -a $LOG_FILE) | |
exec 2>&1 | |
} | |
log_setup | |
# Function: Log an event. | |
log() { | |
echo "[$(date +"%Y-%m-%d"+"%T")]: $*" | |
} | |
TEST_FILE_NAME=$(/usr/bin/basename $TEST_FILE) | |
UPLOAD_SPEED=$(/usr/bin/script -q -c "scp $TEST_FILE $DEST_SERVER:$DEST_LOCATION"|grep $TEST_FILE_NAME|awk '{print $(NF-2),"\t",$(NF-1),"\t",$NF}') | |
DOWNLOAD_SPEED=$(/usr/bin/script -q -c "scp $DEST_SERVER:$DEST_LOCATION/$TEST_FILE_NAME $LOCAL_LOCATION"|grep $TEST_FILE_NAME|awk '{print $(NF-2),"\t",$(NF-1),"\t",$NF}') | |
log "upload $UPLOAD_SPEED" | |
log "download $DOWNLOAD_SPEED" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment