Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed to ookla speedtest-cli #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 60 additions & 18 deletions netcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ PRINT_HELP() {
echo "$VAR_SCRIPTNAME -c Check connection ever (n) seconds. Default is 5"
echo "$VAR_SCRIPTNAME -u URL/Host to check, default is http://www.google.com"
echo "$VAR_SCRIPTNAME -w Enable the remote webinteface"
echo "$VAR_SCRIPTNAME -p Specify an optional port for the webinterface"
echo "$VAR_SCRIPTNAME -p Specify an optional port for the webinterface"
echo "$VAR_SCRIPTNAME -i Install netcheck as a system service"
echo "$VAR_SCRIPTNAME -d path/script Specify script to execute on disconnect"
echo "$VAR_SCRIPTNAME -r path/script Specify script to execute on reconnect"
Expand Down Expand Up @@ -147,7 +147,7 @@ START_WEBSERVER() {
VAR_PYTHON_VERSION=$($VAR_PYTHON_EXEC -c 'import sys; print(sys.version_info[0])')
case $VAR_PYTHON_VERSION in
2)
(cd $VAR_SCRIPTLOC/log; $VAR_PYTHON_EXEC -m SimpleHTTPServer $1 &) &> /dev/null
(cd $VAR_SCRIPTLOC/log; $VAR_PYTHON_EXEC -m SimpleHTTPServer $1 &) &> /dev/null
;;
3)
(cd $VAR_SCRIPTLOC/log; $VAR_PYTHON_EXEC -m http.server $1 &) &> /dev/null
Expand Down Expand Up @@ -176,38 +176,72 @@ SETUP_WEBSERVER() {
}

CHECK_FOR_SPEEDTEST() {
if [[ $VAR_SPEEDTEST_DISABLED = false ]]; then :
if [ -f "$VAR_SCRIPTLOC/speedtest-cli.py" ] || [ -f "$VAR_SCRIPTLOC/speedtest-cli" ]; then
echo -e "SpeedTest-CLI: $COLOR_GREEN Installed $COLOR_RESET"
VAR_SPEEDTEST_READY=true
if [[ $VAR_SPEEDTEST_DISABLED = false ]]; then
if command -v speedtest &> /dev/null; then
echo -e "SpeedTest-CLI: $COLOR_GREEN Installed $COLOR_RESET"
VAR_SPEEDTEST_READY=true
else
echo -e "SpeedTest-CLI: $COLOR_RED Not Installed $COLOR_RESET"
INSTALL_SPEEDTEST
fi
if [ -f "$VAR_SCRIPTLOC/speedtest-cli" ]; then
mv $VAR_SCRIPTLOC/speedtest-cli $VAR_SCRIPTLOC/speedtest-cli.py
echo -e "SpeedTest-CLI: $COLOR_RED Not Installed $COLOR_RESET"
INSTALL_SPEEDTEST
fi
else
echo -e "SpeedTest-CLI: $COLOR_RED Disabled $COLOR_RESET"
echo -e "SpeedTest-CLI: $COLOR_RED Disabled $COLOR_RESET"
fi
}


INSTALL_SPEEDTEST() {
PRINT_INSTALL
read -r response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
PRINT_INSTALLING
wget -q -O "$VAR_SCRIPTLOC/speedtest-cli.py" https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod +x "$VAR_SCRIPTLOC/speedtest-cli.py"
sudo apt-get install -y curl
curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
sudo apt-get update
sudo apt-get install -y speedtest-cli
PRINT_NL
CHECK_FOR_SPEEDTEST
else
VAR_SPEEDTEST_DISABLED=true
fi
}


RUN_SPEEDTEST() {
$VAR_SCRIPTLOC/speedtest-cli.py --simple --secure | sed 's/^/ /' | tee -a $VAR_LOGFILE
# $VAR_SCRIPTLOC/speedtest-cli.py --simple --secure | sed 's/^/ /' | tee -a $VAR_LOGFILE
local attempts=10
local speedtest_output
local retry_delay=4 # Delay in seconds

echo "Starting speed test..."

# Loop to run speedtest multiple times or until download speed is obtained
while [[ $attempts -gt 0 ]]; do
speedtest_output="$(speedtest)"

# Check if speedtest output contains download speed
if grep -q "Mbps" <<< "$speedtest_output"; then
break
fi

((attempts--))
sleep $retry_delay # Add delay between retries
done

# If download speed was obtained, extract and log upload speed, download speed, and latency
if grep -q "Mbps" <<< "$speedtest_output"; then
upload=$(echo "$speedtest_output" | awk '/Upload:/ { print $2, $3 }')
download=$(echo "$speedtest_output" | awk '/Download:/ { print $2, $3 }')
latency=$(echo "$speedtest_output" | awk '/Idle Latency:/ { print $3, $4 }')
packet_loss=$(echo "$speedtest_output" | awk '/Packet Loss:/ { print $3 }')

echo "Upload: $upload" | tee -a "$VAR_LOGFILE"
echo "Download: $download" | tee -a "$VAR_LOGFILE"
echo "Latency: $latency" | tee -a "$VAR_LOGFILE"
echo "Packet Loss: $packet_loss" | tee -a "$VAR_LOGFILE"
else
echo "Failed to obtain download speed" | tee -a "$VAR_LOGFILE"
fi
}

NET_CHECK() {
Expand Down Expand Up @@ -260,7 +294,7 @@ INSTALL_AS_SERVICE() {
echo "Netcheck can only be installed as a service on systems using systemctl."
echo "You will need to manually setup Netcheck as a service on your system."
exit
else
else
FILE=/etc/systemd/system/netcheck.service
if [ -f "$FILE" ]; then
echo "Netcheck already installed as a service."
Expand All @@ -269,17 +303,25 @@ INSTALL_AS_SERVICE() {
else
echo "You will need to authenticate using sudo to install."
echo "Installing netcheck as a service..."

# Get current user and group
CURRENT_USER=$(id -u -n)
CURRENT_GROUP=$(id -g -n)

sudo tee -a /etc/systemd/system/netcheck.service <<EOL >/dev/null
[Unit]
Description=Netcheck Service

[Service]
User=$CURRENT_USER
Group=$CURRENT_GROUP
WorkingDirectory=$VAR_SCRIPTLOC/
ExecStart=$VAR_SCRIPTLOC/$VAR_SCRIPTNAME

[Install]
WantedBy=multi-user.target
EOL

sudo systemctl enable netcheck.service >/dev/null
PRINT_MANAGESERVICE
echo "Would you like to start netcheck as a service now?"
Expand Down Expand Up @@ -346,7 +388,7 @@ while getopts "f:d:r:t:c:u:p:whelp-sie" opt; do
s)
VAR_SPEEDTEST_DISABLED=true
;;
i)
i)
VAR_INSTALL_AS_SERVICE=true
;;
e)
Expand Down Expand Up @@ -380,4 +422,4 @@ if [[ $VAR_SPEEDTEST_READY = true ]]; then :
RUN_SPEEDTEST
PRINT_HR | tee -a $VAR_LOGFILE
fi
NET_CHECK
NET_CHECK