Skip to content
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
206 changes: 130 additions & 76 deletions Network-Diagnostic-Scripts/Wifi-Diagnostic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ QUALITY_THRESHOLD=50
LOGFILE="$HOME/wifi_diagnosis.log"

# Global variables to store network environment data
SSID=""
SIGNAL_STRENGTH=""
SIGNAL_LEVEL=""
WIFI_FREQUENCY=""
WIFI_INTERFACE=""
SSID="N/A"
SIGNAL_STRENGTH="N/A"
SIGNAL_LEVEL="N/A"
WIFI_FREQUENCY="N/A"
VPN_ACTIVE=""
VPN_TYPE=""
VPN_INTERFACE=""
POWER_SAVE=""
POWER_SAVE="Unable to determine"

# Function to log and display output
log_and_display() {
Expand All @@ -76,38 +77,55 @@ if [ -f "$LOGFILE" ]; then
rm "$LOGFILE"
fi

# Check for internet connectivity and display ping results
# Print rfkill diagnostics and recommendations before exiting on connectivity failure
print_rfkill_and_exit() {
log_and_display_plain "rfkill Status:"
RFKILL_STATUS=$(rfkill list 2>&1)
log_and_display_plain "$RFKILL_STATUS"

log_and_display "Final Recommendations"
log_and_display "====================="

# Explain rfkill status
log_and_display_plain "rfkill status explanation:"
if echo "$RFKILL_STATUS" | grep -q "Soft blocked: yes"; then
log_and_display_plain "- Soft blocked: Yes. This means the Wi-Fi is disabled by software. To unblock, run: ${YELLOW}rfkill unblock wifi${RESET}"
fi
if echo "$RFKILL_STATUS" | grep -q "Hard blocked: yes"; then
log_and_display_plain "- Hard blocked: Yes. This means the Wi-Fi is disabled by a physical switch. Check for a Wi-Fi switch on your device and turn it on."
fi
if echo "$RFKILL_STATUS" | grep -q "Soft blocked: no" && echo "$RFKILL_STATUS" | grep -q "Hard blocked: no"; then
log_and_display_plain "- Wi-Fi is not blocked by rfkill. The issue might be related to drivers or hardware."
fi

log_and_display_plain "- After addressing these issues, rerun the script to perform a full diagnosis."
exit 1
}

# Check for internet connectivity over the Wi-Fi interface and display ping results
check_internet_connection() {
log_and_display_plain "Performing ping test to 8.8.8.8..."
PING_RESULTS=$(ping -c 4 8.8.8.8 2>&1)
echo "$PING_RESULTS" >> "$LOGFILE"
WIFI_INTERFACE=$(iw dev 2>/dev/null | awk '$1=="Interface"{print $2; exit}')
if [ -z "$WIFI_INTERFACE" ]; then
log_and_display_plain "No Wi-Fi interface detected via 'iw dev'. Exiting."
print_rfkill_and_exit
fi

log_and_display_plain "Performing ping test to 8.8.8.8 over Wi-Fi interface ($WIFI_INTERFACE)..."
PING_RESULTS=$(ping -I "$WIFI_INTERFACE" -c 4 -W 3 8.8.8.8 2>&1)
ping_exit=$?
log_and_display_plain "$PING_RESULTS"
log_and_display_plain
if echo "$PING_RESULTS" | grep -q "0% packet loss"; then
if [ "$ping_exit" -eq 0 ] && echo "$PING_RESULTS" | grep -qE '(^|[^0-9])0% packet loss'; then
return 0
else
log_and_display_plain "No network detected. Exiting."
log_and_display_plain "rfkill Status:"
RFKILL_STATUS=$(rfkill list)
log_and_display_plain "$RFKILL_STATUS"

log_and_display "Final Recommendations"
log_and_display "====================="

# Explain rfkill status
log_and_display_plain "rfkill status explanation:"
if echo "$RFKILL_STATUS" | grep -q "Soft blocked: yes"; then
log_and_display_plain "- Soft blocked: Yes. This means the Wi-Fi is disabled by software. To unblock, run: ${YELLOW}rfkill unblock wifi${RESET}"
fi
if echo "$RFKILL_STATUS" | grep -q "Hard blocked: yes"; then
log_and_display_plain "- Hard blocked: Yes. This means the Wi-Fi is disabled by a physical switch. Check for a Wi-Fi switch on your device and turn it on."
log_and_display_plain "No connectivity detected over Wi-Fi interface ($WIFI_INTERFACE). Exiting."
default_routes=$(ip route show default 2>/dev/null)
if [ -n "$default_routes" ] && echo "$default_routes" | grep -vq "dev $WIFI_INTERFACE"; then
log_and_display_plain "Note: Another network interface has an active default route, but Wi-Fi ($WIFI_INTERFACE) could not reach 8.8.8.8."
fi
if echo "$RFKILL_STATUS" | grep -q "Soft blocked: no" && echo "$RFKILL_STATUS" | grep -q "Hard blocked: no"; then
log_and_display_plain "- Wi-Fi is not blocked by rfkill. The issue might be related to drivers or hardware."
fi

log_and_display_plain "- After addressing these issues, rerun the script to perform a full diagnosis."
exit 1
log_and_display_plain "Wi-Fi link status ($WIFI_INTERFACE):"
log_and_display_plain "$(iw dev "$WIFI_INTERFACE" link 2>&1)"
print_rfkill_and_exit
fi
}

Expand Down Expand Up @@ -169,15 +187,18 @@ check_system_info() {
# Function to check Wi-Fi interfaces
check_wifi_interfaces() {
log_and_display_plain "Wi-Fi Interface Information:"
iw dev 2>/dev/null | awk '$1=="Interface"{print $2}' | while read -r interface; do
while read -r interface; do
[ -z "$interface" ] && continue
WIFI_INTERFACE="${WIFI_INTERFACE:-$interface}"
log_and_display_plain "Interface: $interface"
iw dev "$interface" info 2>/dev/null | tee -a "$LOGFILE"
link_info=$(iw dev "$interface" link 2>/dev/null)
log_and_display_plain "$link_info"

if echo "$link_info" | grep -q "Not connected"; then
if [ -z "$link_info" ] || echo "$link_info" | grep -q "Not connected"; then
SSID="N/A"
SIGNAL_STRENGTH="N/A"
SIGNAL_LEVEL="N/A"
WIFI_FREQUENCY="N/A"
else
SSID=$(echo "$link_info" | awk '/SSID/{print $2}')
Expand Down Expand Up @@ -208,7 +229,7 @@ check_wifi_interfaces() {
echo "POWER_SAVE=$POWER_SAVE" >> "$LOGFILE"

log_and_display_plain
done
done < <(iw dev 2>/dev/null | awk '$1=="Interface"{print $2}')

# Check for VPN (tun0 or wg0) interfaces
if ip link show tun0 &>/dev/null; then
Expand Down Expand Up @@ -249,20 +270,32 @@ show_loading_bar() {
# Function to perform speed test
perform_speed_test() {
log_and_display_plain "Network Speed Test:"
log_and_display_plain "Running speed test, please wait... ${YELLOW}(Note: The test may appear to hang at 98% for a few minutes. This is normal - wait patiently.)${RESET}"
local wifi_ip=""
if [ -n "$WIFI_INTERFACE" ]; then
wifi_ip=$(ip -4 -o addr show dev "$WIFI_INTERFACE" 2>/dev/null | awk '{split($4,a,"/"); print a[1]; exit}')
fi
if [[ -z "$wifi_ip" || -z "$SSID" || "$SSID" == "N/A" ]]; then
log_and_display_plain "Skipping speed test: Wi-Fi interface (${WIFI_INTERFACE:-unknown}) has no active IPv4 connection."
log_and_display_plain
return
fi

log_and_display_plain "Running speed test over $WIFI_INTERFACE ($wifi_ip), please wait... ${YELLOW}(Note: The test may appear to hang at 98% for a few minutes. This is normal - wait patiently.)${RESET}"

# Start the loading bar in the background
show_loading_bar 60 &
loading_pid=$!

# Run the speed test
speed_test=$(speedtest-cli --secure 2>/dev/null)
# Run the speed test bound to the Wi-Fi interface IP
speed_test=$(speedtest-cli --secure --source "$wifi_ip" 2>/dev/null)
speed_test_exit_code=$?

# Stop the loading bar
kill $loading_pid 2>/dev/null
wait $loading_pid 2>/dev/null
printf "\r%$(tput cols)s\r" # Clear the line
local cols
cols=$(tput cols 2>/dev/null || echo 80)
printf "\r%${cols}s\r" "" # Clear the line

if [ $speed_test_exit_code -eq 0 ]; then
log_and_display_plain "$speed_test"
Expand All @@ -285,7 +318,10 @@ check_nmcli_status() {
nmcli dev wifi list 2>/dev/null | tee -a "$LOGFILE"

# Check connection speed using iw
connection_speed=$(iw dev $(iw dev | awk '$1=="Interface"{print $2}') link | grep 'tx bitrate' | awk '{print $3, $4}')
connection_speed=""
if [ -n "$WIFI_INTERFACE" ]; then
connection_speed=$(iw dev "$WIFI_INTERFACE" link 2>/dev/null | grep 'tx bitrate' | awk '{print $3, $4}')
fi

if [[ -n "$connection_speed" ]]; then
log_and_display_plain "Current connection speed: $connection_speed"
Expand Down Expand Up @@ -356,28 +392,34 @@ summarize_findings() {
# Add Network Environment section
# Detect the Wi-Fi interface if not set
if [ -z "$interface" ]; then
interface=$(iw dev | awk '$1=="Interface"{print $2; exit}')
interface="${WIFI_INTERFACE:-$(iw dev 2>/dev/null | awk '$1=="Interface"{print $2; exit}')}"
fi

# Get link information
link_info=$(iw dev "$interface" link)

# Extract the frequency
frequency2=$(iw dev "$interface" link | awk '/freq/ {print $2}')

signal_strength=$(iw dev "$interface" link | awk '/signal:/ {print $2}')
link_info=""
frequency2=""
signal_strength=""
if [ -n "$interface" ]; then
link_info=$(iw dev "$interface" link 2>/dev/null)
frequency2=$(echo "$link_info" | awk '/freq:/ {print $2}')
signal_strength=$(echo "$link_info" | awk '/signal:/ {print $2}')
fi

# Retrieve POWER_SAVE from log file
POWER_SAVE=$(grep "POWER_SAVE=" "$LOGFILE" | tail -n1 | cut -d'=' -f2)
POWER_SAVE=$(grep "POWER_SAVE=" "$LOGFILE" 2>/dev/null | tail -n1 | cut -d'=' -f2)

# Display the network environment information
signal_strength_display="N/A"
[ -n "$signal_strength" ] && signal_strength_display="${signal_strength} dBm"
frequency_display="N/A"
[ -n "$frequency2" ] && frequency_display="${frequency2} MHz"
log_and_display "Network Environment"
log_and_display "===================="
log_and_display_plain "- SSID: $SSID"
log_and_display_plain "- Signal Strength: $signal_strength dBm"
log_and_display_plain "- Signal level: $SIGNAL_LEVEL%"
log_and_display_plain "- Frequency: $frequency2 MHz"
log_and_display_plain "- $POWER_SAVE"
log_and_display_plain "- SSID: ${SSID:-N/A}"
log_and_display_plain "- Signal Strength: $signal_strength_display"
log_and_display_plain "- Signal level: ${SIGNAL_LEVEL:-N/A}%"
log_and_display_plain "- Frequency: $frequency_display"
log_and_display_plain "- ${POWER_SAVE:-Power save: Unknown}"
log_and_display_plain "- VPN Active: $VPN_ACTIVE"
if [ "$VPN_ACTIVE" == "Yes" ]; then
log_and_display_plain "- VPN Type: $VPN_TYPE ($VPN_INTERFACE)"
Expand All @@ -395,60 +437,72 @@ summarize_findings() {

# Wi-Fi Interface INSIGHTS
log_and_display_plain "Wi-Fi Interface:"
if [ "$SSID" != "N/A" ]; then
if [[ -n "$SSID" && "$SSID" != "N/A" ]]; then
log_and_display_plain "- Connected to network: $SSID"

if [ "$(echo "$frequency2 < 2500" | bc -l)" -eq 1 ]; then
log_and_display_plain "- You are on the 2.4 GHz band. This has better range but might be more congested."
elif [ "$(echo "$frequency2 >= 5150 && $frequency2 <= 5875" | bc -l)" -eq 1 ]; then
log_and_display_plain "- You are on the 5 GHz band. This typically provides faster speeds but has shorter range."
elif [ "$(echo "$frequency2 >= 5925 && $frequency2 <= 7125" | bc -l)" -eq 1 ]; then
log_and_display_plain "- You are on the 6 GHz band. This typically provides even faster speeds but may have a shorter range."
if [[ "$frequency2" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
if [[ "$(echo "$frequency2 < 2500" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- You are on the 2.4 GHz band. This has better range but might be more congested."
elif [[ "$(echo "$frequency2 >= 5150 && $frequency2 <= 5875" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- You are on the 5 GHz band. This typically provides faster speeds but has shorter range."
elif [[ "$(echo "$frequency2 >= 5925 && $frequency2 <= 7125" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- You are on the 6 GHz band. This typically provides even faster speeds but may have a shorter range."
else
log_and_display_plain "- Unknown frequency band. Characteristics are unknown."
fi
else
log_and_display_plain "- Unknown frequency band. Characteristics are unknown."
log_and_display_plain "- Frequency information unavailable."
fi

signal_abs=$(echo "$signal_strength" | tr -d '-')
if [ "$(echo "$signal_abs < 50" | bc -l)" -eq 1 ]; then
log_and_display_plain "- Excellent signal strength. You should experience optimal performance."
elif [ "$(echo "$signal_abs < 60" | bc -l)" -eq 1 ]; then
log_and_display_plain "- Good signal strength. Should be sufficient for most applications."
elif [ "$(echo "$signal_abs < 70" | bc -l)" -eq 1 ]; then
log_and_display_plain "- Fair signal strength. You might experience some slowdowns."
if [[ "$signal_abs" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
if [[ "$(echo "$signal_abs < 50" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Excellent signal strength. You should experience optimal performance."
elif [[ "$(echo "$signal_abs < 60" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Good signal strength. Should be sufficient for most applications."
elif [[ "$(echo "$signal_abs < 70" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Fair signal strength. You might experience some slowdowns."
else
log_and_display_plain "- Poor signal strength. Consider moving closer to your router or checking for obstacles."
fi
else
log_and_display_plain "- Poor signal strength. Consider moving closer to your router or checking for obstacles."
log_and_display_plain "- Signal strength information unavailable."
fi

tx_bitrate=$(echo "$link_info" | awk '/tx bitrate/{print $3}')
if [ "$(echo "$tx_bitrate > 100" | bc -l)" -eq 1 ]; then
log_and_display_plain "- Good transmission rate. Suitable for most high-bandwidth activities."
if [[ "$tx_bitrate" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
if [[ "$(echo "$tx_bitrate > 100" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Good transmission rate. Suitable for most high-bandwidth activities."
else
log_and_display_plain "- Lower transmission rate. You might experience slowdowns with high-bandwidth activities."
fi
else
log_and_display_plain "- Lower transmission rate. You might experience slowdowns with high-bandwidth activities."
log_and_display_plain "- Transmission rate information unavailable."
fi
else
log_and_display_plain "- This interface is not connected to any network."
fi

# Speed Test INSIGHTS
log_and_display_plain "Speed Test:"
if [[ -n "$ping" && -n "$download" && -n "$upload" ]]; then
if [ "$(echo "$ping < 20" | bc -l)" -eq 1 ]; then
if [[ "$ping" =~ ^[0-9]+(\.[0-9]+)?$ && "$download" =~ ^[0-9]+(\.[0-9]+)?$ && "$upload" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
if [[ "$(echo "$ping < 20" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Excellent ping time. Great for real-time applications like gaming or video calls."
elif [ "$(echo "$ping < 50" | bc -l)" -eq 1 ]; then
elif [[ "$(echo "$ping < 50" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Good ping time. Suitable for most online activities."
else
log_and_display_plain "- Higher ping time. You might experience lag in real-time applications."
fi

if [ "$(echo "$download > 100" | bc -l)" -eq 1 ]; then
if [[ "$(echo "$download > 100" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Fast download speed. Excellent for streaming, large file downloads, and multiple users. Note: Speed test results should be compared to other services such as Fast.com and Google Speed Test if the results feel wrong."
elif [ "$(echo "$download > 25" | bc -l)" -eq 1 ]; then
elif [[ "$(echo "$download > 25" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Good download speed. Suitable for most online activities and HD streaming. Note: Speed test results should be compared to other services such as Fast.com and Google Speed Test if the results feel wrong."
else
log_and_display_plain "- Lower download speed. You might experience buffering with HD streaming or slow file downloads. Note: Speed test results should be compared to other services such as Fast.com and Google Speed Test if the results feel wrong."
fi

if [ "$(echo "$upload > 20" | bc -l)" -eq 1 ]; then
if [[ "$(echo "$upload > 20" | bc -l 2>/dev/null)" == "1" ]]; then
log_and_display_plain "- Good upload speed. Suitable for video calls, uploading large files, and online backups."
else
log_and_display_plain "- Lower upload speed. You might experience issues with video calls or uploading large files."
Expand Down