One of our members on the SLUG mailing list asked if anyone knew of a good way of notifying them when Internet connectivity is lost, in much of the same way as the network manager informs you that it has lost a local network connection, by displaying a notification in the system tray. I had a quick search of the Internet but couldn’t find anything useful, so I thought I would roll my own version using Python and GTK.
Basically the Python script below places an icon blue globe in the system tray when the PC is able to connect to the Internet and flashes a red stop icon when Internet connection is lost. To find out whether it has connection to the Internet it pings Googles DNS server (8.8.8.8) every 10 seconds. There are a number of settings that be configured on lines 16 to 22 but the rest of the code should just work. This was developed and tested on Linux Mint 19.2 Cinnamon Version 4.2.4.
Copy the Python code below into a file internetSystemTrayStatus.py and do a
chmod a+x internetSystemTrayStatus.py
and then to run it ./internetSystemTrayStatus.py &
#!/usr/bin/env python """ Author: Ian Crane Date: 24 Nov 2019 Description: Displays a icon of the Internet connection status in the system tray of Linux Original code from: <http://files.majorsilence.com/rubbish/pygtk-book/pygtk-notebook-html/pygtk-notebook-latest.html#SECTION00430000000000000000> """ import gtk import os import time import gobject # Application settings connectedIcon = gtk.STOCK_DISCONNECT # Yes this description does seem a little backwards #disconnectedIcon = gtk.STOCK_CONNECT disconnectedIcon = gtk.STOCK_DIALOG_ERROR disconnectedIconFlashing = True internetAddressToPing = "8.8.8.8" pingReplyCount = "1" # You may need to increase this if you have a flakey internet connection internetCheckIntervalSeconds = 10 def message(data=None): "Function to display messages to the user." msg=gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, data) msg.run() msg.destroy() def close_app(data=None): #systemTrayIcon.set_from_stock(gtk.STOCK_CONNECT) #message(data) gtk.main_quit() def make_menu(event_button, event_time, data=None): menu = gtk.Menu() close_item = gtk.MenuItem("Close App") #Append the menu items menu.append(close_item) #Add callbacks close_item.connect_object("activate", close_app, "Close App") #Show the menu items close_item.show() #Popup the menu menu.popup(None, None, None, event_button, event_time) def on_right_click(data, event_button, event_time): make_menu(event_button, event_time) def on_left_click(event): message("Internet Connectivity monitor\n\nThis program shows the connectivity status of this PC to the Internet.\n\nInternet address to ping = " + internetAddressToPing + "\nPing reply count = " + pingReplyCount + "\nInternet check interval seconds = " + str(internetCheckIntervalSeconds) + "\nFlash disconnected icon = " + str(disconnectedIconFlashing) + "\n\nThese settings can be changed by modifying the parameters in the top of of the Python file " + __file__) def ping_internet(): pingResponse = os.system("ping -c " + pingReplyCount + " " + internetAddressToPing + " > /dev/null 2>&1") if pingResponse == 0: #Host responed to ping systemTrayIcon.set_blinking(False) systemTrayIcon.set_from_stock(connectedIcon) systemTrayIcon.set_tooltip("Connected to Internet.") systemTrayIcon.set_visible(True) else: #Host didn't respond to ping systemTrayIcon.set_from_stock(disconnectedIcon) systemTrayIcon.set_blinking(disconnectedIconFlashing) systemTrayIcon.set_tooltip("Disconnected from Internet") systemTrayIcon.set_visible(True) gobject.timeout_add(internetCheckIntervalSeconds*1000,ping_internet) if __name__ == '__main__': systemTrayIcon = gtk.status_icon_new_from_stock(disconnectedIcon) systemTrayIcon.set_tooltip("Waiting for Internet status") systemTrayIcon.set_visible(True) systemTrayIcon.connect('popup-menu', on_right_click) systemTrayIcon.connect('activate', on_left_click) gobject.timeout_add(internetCheckIntervalSeconds*1000,ping_internet) gtk.main()