Skip to content

Instantly share code, notes, and snippets.

@hleroy
Created December 5, 2023 08:30
Show Gist options
  • Save hleroy/e00e26caaf0e10fcd60e37f66f3a9a95 to your computer and use it in GitHub Desktop.
Save hleroy/e00e26caaf0e10fcd60e37f66f3a9a95 to your computer and use it in GitHub Desktop.
Display WiFi status monitor script using Bash
#!/bin/bash
# The name of the wireless interface (e.g., wlp2s0). You may need to change this.
INTERFACE="wlp2s0"
# URL to check for internet connectivity
CHECK_URL="http://www.google.com"
# Function to check internet connectivity
check_internet() {
# Check if we can fetch a page from a known URL
if curl --connect-timeout 5 --head $CHECK_URL &> /dev/null; then
echo -e "\e[32mOnline\e[0m" # Green color for "Online"
else
echo -e "\e[31mOffline\e[0m" # Red color for "Offline"
fi
}
# Function to get WiFi information using nmcli
get_wifi_info() {
# Get the line with the current connection from nmcli
NMCLI_LINE=$(nmcli -t -f IN-USE,SSID,BSSID,MODE,CHAN,RATE,SIGNAL device wifi | grep '^\*')
# Extracting information using awk
SSID=$(echo "$NMCLI_LINE" | awk -F: '{print $2}')
# BSSID=$(echo "$NMCLI_LINE" | awk -F: '{printf "%s:%s:%s:%s:%s:%s", $3, $4, $5, $6, $7, $8}')
BSSID=$(echo "$NMCLI_LINE" | awk -F: '{printf "%s:%s:%s:%s:%s:%s", $3, $4, $5, $6, $7, $8}' | sed 's/\\//g')
MODE=$(echo "$NMCLI_LINE" | awk -F: '{print $9}')
CHANNEL=$(echo "$NMCLI_LINE" | awk -F: '{print $10}')
RATE=$(echo "$NMCLI_LINE" | awk -F: '{print $11}')
SIGNAL=$(echo "$NMCLI_LINE" | awk -F: '{print $12}')
# Get IP Address
IP=$(ip -f inet addr show $INTERFACE | grep -oP 'inet \K[\d.]+' || echo "N/A")
# Check Internet Connectivity
INTERNET_STATUS=$(check_internet)
# Print the output
echo "SSID: $SSID, BSSID: $BSSID, IP: $IP, Mode: $MODE, Channel: $CHANNEL, Rate: $RATE, Signal: ${SIGNAL}%, Internet: $INTERNET_STATUS"
}
# Infinite loop to get data every second
while true; do
get_wifi_info
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment