Skip to content

Instantly share code, notes, and snippets.

@mie00
Last active April 14, 2019 04:23
Show Gist options
  • Save mie00/c70b60eda9c844bf553927bf747438bb to your computer and use it in GitHub Desktop.
Save mie00/c70b60eda9c844bf553927bf747438bb to your computer and use it in GitHub Desktop.
Check internet connection script
#!/bin/bash
export COLOR_NC='\e[0m'
export COLOR_GREEN='\e[0;32m'
export COLOR_RED='\e[0;31m'
export INTERFACE='wlp3s0'
if [ "`nmcli networking`" == "enabled" ]; then
printf "${COLOR_GREEN}networking enabled${COLOR_NC}\n"
else
printf "${COLOR_RED}networking disabled${COLOR_NC}\n"
fi
if [ "`nmcli radio wifi`" == "enabled" ]; then
printf "${COLOR_GREEN}wifi enabled${COLOR_NC}\n"
else
printf "${COLOR_RED}wifi disabled${COLOR_NC}\n"
fi
CONN="`nmcli connection show --active | grep $INTERFACE`"
if [ -n "$CONN" ]; then
printf "${COLOR_GREEN}wifi connected to $CONN${COLOR_NC}\n"
else
printf "${COLOR_RED}wifi not connected${COLOR_NC}\n"
fi
ADDR="`ip address show dev $INTERFACE scope global | grep inet`"
if [ -n "$ADDR" ]; then
printf "${COLOR_GREEN}have address $ADDR${COLOR_NC}\n"
else
printf "${COLOR_RED}no address${COLOR_NC}\n"
fi
DEFAULT="`ip route | grep default`"
if [ -n "$DEFAULT" ]; then
printf "${COLOR_GREEN}have default route $DEFAULT${COLOR_NC}\n"
else
printf "${COLOR_RED}no default route${COLOR_NC}\n"
fi
GW="`echo $DEFAULT | cut -d ' ' -f 3`"
GWWLP="`ip route get $GW | grep $INTERFACE`"
if [ -n "$GWWLP" ]; then
printf "${COLOR_GREEN}gw set correctly${COLOR_NC}\n"
else
printf "${COLOR_RED}gw not set correctly${COLOR_NC}\n"
fi
if ping -W 1 -c 1 "$GW"; then
printf "${COLOR_GREEN}gw connected${COLOR_NC}\n"
else
printf "${COLOR_RED}gw not connected${COLOR_NC}\n"
fi
if ping -W 1 -c 1 "8.8.8.8"; then
printf "${COLOR_GREEN}8.8.8.8 connected${COLOR_NC}\n"
else
printf "${COLOR_RED}8.8.8.8 not connected${COLOR_NC}\n"
fi
if ping -W 1 -c 1 "google.com"; then
printf "${COLOR_GREEN}google.com connected${COLOR_NC}\n"
else
printf "${COLOR_RED}google.com not connected${COLOR_NC}\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment