
Have you ever wanted to know whether your computer / laptop is still connected to the internet without opening up a browser? Displaying the status of Internet connectivity in your Waybar as an icon is a cool and quick way to do the same.
Ping Command
We will be making use of the Ping command to check whether the computer is indeed connected to the internet. As you might know, ping is a utility available in Linux which is used to send ping packets to the given host. If the host is reachable, the host replies to the ping packets.
ping also displays other useful information such as the round trip time (RTT) in milliseconds. This is what is usually referred to as latency in video games. It is also a good measure of the internet link speed, where smaller ping times means that you have a good connection. This is used to ascertain whether the computer is online.

The following script sends out 3 ping packets to google.com. It then uses the awk command to filter out the last line of the output. It prints the average ping in milli seconds if google.com is accessible. If no internet connection is available, the script prints out ‘disconnected’. You can change these characters so that icons are shown instead, just like the sample image above.
!/bin/zsh
ping -qc 3 google.com 2>&1 | awk -F'/' 'END{print(/^rtt/? "Ping: " int($5) "ms":"disconnected")}'
Save the above as check-online.sh in a suitable location like ‘~/.scripts’. You can modify the code to your liking. For example, the number 3 after “-qc” indicates the number of ping packets to be sent. You can even change it to 1 to get a faster result. The larger the number the more packets are sent resulting in the average being more accurate.
Waybar Config
To display the output of our script in Waybar, we need to make use of a custom module. To add one, simply the following line in either ‘modules-left’ or ‘modules-center’ or ‘modules-right’ section, as per your liking. In the screenshot above, I’ve used it in the ‘modules-right’ section.
"custom/check-online",

Finally, we need to tell Waybar what is to be displayed. Add the following bit in the waybar config file. This tells waybar to execute our check-online.sh script every 20 seconds.
"custom/check-online": {
"format": "{}",
"interval": 20,
"tooltip": true,
"tooltip-format": "Online Status",
"exec": "zsh /home/vitarsi/.scripts/check-online.sh",
},

Restart Waybar and you should now be able to see your computer’s Online status in your status bar.