Wait till internet is available using fping / How to check if internet is available using bash script

Post Free Ads for Agri, Solar, Eco products

Do you want to wait for an internet availability before proceeding further such as send an email from command line only when internet is available.. In those cases, we have to go into infinite loop of checking whether we are really connected to network. Ubuntu has a utility called “fping” we comes handy for such use cases. [ fping – send ICMP ECHO_REQUEST packets to network hosts ]

 $ sudo apt-get install fping 
 $ vim test_network.sh 


#while [ “$(fping google.com | grep alive)” == “” ]
# echo “waiting for internet …”
# sleep 3
# echo “Internet is now online”
# You can either use above code OR
# disable below and enable above from #while to echo

echo “checking if network is available, if not we will wait…”
fping -l | read
echo “Internet is now online”
#—– end of script —–

Now just run this script as,

 $ bash test_network.sh 

fping description from “man page”

fping is a program like ping which uses the Internet Control Message Protocol (ICMP) echo request to
determine if a target host is responding. fping differs from ping in that you can specify any number of
targets on the command line, or specify a file containing the lists of targets to ping. Instead of
sending to one target until it times out or replies, fping will send out a ping packet and move on to the
next target in a round-robin fashion. In the default mode, if a target replies, it is noted and removed
from the list of targets to check; if a target does not respond within a certain time limit and/or retry
limit it is designated as unreachable. fping also supports sending a specified number of pings to a
target, or looping indefinitely (as in ping ). Unlike ping, fping is meant to be used in scripts, so its
output is designed to be easy to parse.

SHUFFLED :   How to check which header belongs to which package in ubuntu

The binary named fping6 is the same as fping, except that it uses IPv6 addresses instead of IPv4.

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded
Tags: ,

Leave a Reply