Skip to content

Instantly share code, notes, and snippets.

@ismet55555
Created October 30, 2020 14:29
Show Gist options
  • Save ismet55555/00c0c10d0dfcbfbbd3281c4b34194a4f to your computer and use it in GitHub Desktop.
Save ismet55555/00c0c10d0dfcbfbbd3281c4b34194a4f to your computer and use it in GitHub Desktop.
Script to Check Internet Connection
#!/bin/bash
##########################################
# Ismet Handzic (2019)
#
# You can add this script as an alias on your system
# - Add this script to directory "/home/<YOUR USERNAME>/.bashrc_scripts
# - In "/home/<YOUR USERNAME>/.bashrc", add:
# alias internet='/home/<YOUR USERNAME>/.bashrc_scripts/check_internet.sh'
# - Use in terminal:
# internet
##########################################
CURRENT_DATETIME=$(date)
echo "[$CURRENT_DATETIME] : INFO : Checking for interent connection"
echo "[$CURRENT_DATETIME] : INFO : Pinging Google's 8.8.8.8 server ..."
while :
do
CURRENT_DATETIME=$(date)
if ping -q -c 1 -W 1 8.8.8.8 > /dev/null
then
break
fi
echo "[$CURRENT_DATETIME] : CRITICAL : No internet connection found. Checking again 2 seconds ..."
sleep 2
done
echo "[$CURRENT_DATETIME] : INFO : Active interent connection detected"
HOSTNAME=$(hostname -I)
echo "[$CURRENT_DATETIME] : INFO : Local IP: $HOSTNAME"
PUBLIC_IP=$(curl -s ifconfig.me)
echo "[$CURRENT_DATETIME] : INFO : Public IP: $PUBLIC_IP"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment