0

I am really having trouble with lack of this feature on Unity for 16.04. If the WiFi is active at router level, but when there is no internet access ( my ISP fails at times ), there is no way I can know about it in Unity for 16.04. Even 2009 born Windows 7 notifies if there is " no internet access ".

Is there any other file manager or DE that I can use ? Or, any modification available for 16.04 Unity ? Thanks.

user227495
  • 4,089
  • 17
  • 56
  • 101
  • ping -c 4 8.8.8.8 if ping is not forbidden.... – Rostislav Kandilarov Nov 04 '16 at 00:16
  • And you can have a look here for some more advanced and script solutions: http://unix.stackexchange.com/questions/190513/shell-scripting-proper-way-to-check-for-internet-connectivity . You add a job in the cron for each minute and some GUI trigger when no internet connection.... This will be self made solution... – Rostislav Kandilarov Nov 04 '16 at 00:25
  • Thanks @RostislavKandilarov , what does the ping command do ? Check if there is internet ? What is -c for ? – user227495 Nov 04 '16 at 00:27
  • ping sends ICMP packet to google dns server, -c is for count (number) of packets to be sent. – Rostislav Kandilarov Nov 04 '16 at 00:32
  • From the link you gave, in which format should I save this code if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then echo "IPv4 is up" else echo "IPv4 is down" fi – user227495 Nov 04 '16 at 00:32
  • Save it in text file named internet_check.sh for example. And give it exec permissions chmod +x internet_check.sh – Rostislav Kandilarov Nov 04 '16 at 00:46

2 Answers2

2

The simplest way is to use ping -c4 google.com to test your internet connection. If it can reach out beyond your router to any url, then you have internet access. This can easily be adapted to a script that periodically pings a site you choose. But I've a slightly different idea.

Here's a top-panel indicator that'll periodically request internet connection. If your internet connection goes down, it's icon will change to warning sign. It uses standard icon names, so no need to add any additional icons.

Save this as file, make sure it has executable permissions with chmod +x interwebs-indicator (from terminal) or via right-click menu in file manager. Run manually in terminal as

python3 intwerwebs-indicator

or

./interwebs-indicator

if have given it executable permissions.

You can make it start automatically on GUI login ,too.

Simple and easy to use.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#
# Author: Serg Kolo , contact: 1047481448@qq.com
# Date: November 3rd, 2016
# Purpose: appindicator for testing internet connection
# Tested on: Ubuntu 16.04 LTS
#
#
# Licensed under The MIT License (MIT).
# See included LICENSE file or the notice below.
#
# Copyright © 2016 Sergiy Kolodyazhnyy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import gi
gi.require_version('AppIndicator3', '0.1')
gi.require_version('Notify', '0.7')
from gi.repository import GLib as glib
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Gtk as gtk
from gi.repository import Gio
from gi.repository import Gdk
import urllib.request as urllib2

class InterwebsIdicator(object):

    def __init__(self):
        self.app = appindicator.Indicator.new(
            'interwebs-indicator', "gtk-network",
            appindicator.IndicatorCategory.HARDWARE
        )

        self.app.set_attention_icon('dialog-warning')
        #self.app.set_status(appindicator.IndicatorStatus.ACTIVE)
        self.make_menu()
        self.update()

    def add_menu_item(self, menu_obj, item_type, image, label, action, args):
        """ dynamic function that can add menu items depending on
            the item type and other arguments"""
        menu_item, icon = None, None
        if item_type is gtk.ImageMenuItem and label:
            menu_item = gtk.ImageMenuItem.new_with_label(label)
            menu_item.set_always_show_image(True)
            if '/' in image:
                icon = gtk.Image.new_from_file(image)
            else:
                icon = gtk.Image.new_from_icon_name(image, 48)
            menu_item.set_image(icon)
        elif item_type is gtk.ImageMenuItem and not label:
            menu_item = gtk.ImageMenuItem()
            menu_item.set_always_show_image(True)
            if '/' in image:
                icon = gtk.Image.new_from_file(image)
            else:
                icon = gtk.Image.new_from_icon_name(image, 16)
            menu_item.set_image(icon)
        elif item_type is gtk.MenuItem:
            menu_item = gtk.MenuItem(label)
        elif item_type is gtk.SeparatorMenuItem:
            menu_item = gtk.SeparatorMenuItem()
        if action:
            menu_item.connect('activate', action, *args)

        menu_obj.append(menu_item)
        menu_item.show()


    def add_submenu(self,top_menu,label):
        menuitem = gtk.MenuItem(label)
        submenu = gtk.Menu()
        menuitem.set_submenu(submenu)
        top_menu.append(menuitem)
        menuitem.show()
        return submenu

    def make_menu(self):

        self.app_menu = gtk.Menu()
        self.add_menu_item(self.app_menu,gtk.ImageMenuItem,'exit','quit',self.quit,[None])
        self.app.set_menu(self.app_menu)

    def check_connection(self,*args):
        try:
            url = urllib2.urlopen('http://google.com')
            page = url.read()
        except urllib2.HTTPError:
            print('>>> err:')
            self.app.set_status(appindicator.IndicatorStatus.ATTENTION)
            #self.app.attention-icon('network-error')
        except Exception as e:
            print('>>> exception:',e)
        else:
            print('>>> OK')

            self.app.set_status(appindicator.IndicatorStatus.ACTIVE)
            self.app.set_icon('network')


    def callback(self,*args):

        timeout = 5
        glib.timeout_add_seconds(timeout, self.update)

    def update(self,*args):
        self.check_connection() 
        self.callback()
# General purpose functions 


    def quit(self,*args):
        gtk.main_quit()


    def run(self):
        """ Launches the indicator """
        try:
            gtk.main()
        except KeyboardInterrupt:
            pass

    def quit(self, *args):
        """ closes indicator """
        gtk.main_quit()


def main():
    """ defines program entry point """
    indicator = InterwebsIdicator()
    indicator.run()

if __name__ == '__main__':
  try:
    main()
  except  KeyboardInterrupt:
    gtk.main_quit()
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • I ran the script, I got an icon on top panel. The terminal was giving >>Ok continously. Then, I went ahead and unplugged the modem cable to router. Internet was down. But nothing changed. The terminal was giving " Ok " all along and the icon did not gave any warning. What's wrong ? – user227495 Nov 04 '16 at 00:55
  • @user227495 Interesting behavior. It's supposed to give an error , where your machine can't request the URL. I don't have modem - using wifi right now, so can't tell exactly what's wrong. – Sergiy Kolodyazhnyy Nov 04 '16 at 00:59
  • I just tried the updated script you posted. It was also giving the same behaviour. Any work around possible ? – user227495 Nov 04 '16 at 01:01
  • @user227495 Sure, just use while True; do ping -q -c 1 google.com || zenity --info --text "No internet" ; done. I'd still be curious to make this indicator work , for your modem. When your modem cable is disconnected, can you do something like "wget google.com" ? Does it give you HTTP error ? – Sergiy Kolodyazhnyy Nov 04 '16 at 01:06
  • @user227495 I made a small edit. Can you please try and let me know what it says in the command line output ? – Sergiy Kolodyazhnyy Nov 04 '16 at 01:21
  • Ok, give me a minute. :) – user227495 Nov 04 '16 at 01:25
  • I noticed a strange thing. When I tried ping www.google.com , it actually works even when google.co.in does not load on my web browser. I use Google DNS, if that matters. – user227495 Nov 04 '16 at 01:32
  • @user227495 Well, if it's Google's own website, it should work with Google DNS, so I doubt it's DNS issue on your end. – Sergiy Kolodyazhnyy Nov 04 '16 at 01:38
  • I forgot to add, when there was no internet. ie, I unplugged modem input to router. – user227495 Nov 04 '16 at 01:59
  • Well that's very strange, because when there's no internet, you shouldn't be able to ping anything outside of your router. So there's something very wrong with your modem or ISP. – Sergiy Kolodyazhnyy Nov 04 '16 at 02:02
  • Is that blocking the notifications when there is no internet access ? – user227495 Nov 04 '16 at 02:17
  • Can you please update the script so that I can use icons from my own custom icon pack under .icons in home folder ? – user227495 Nov 04 '16 at 02:18
  • @user227495 not quite sure how to do that. Just can change self.app.set_attention_icon('dialog-warning') and "gtk-network", at the beginning of the script. – Sergiy Kolodyazhnyy Nov 04 '16 at 02:21
  • Ok. I will try and update. :) – user227495 Nov 04 '16 at 03:09
  • I replaced google.com with facebook.com. This time, there was no " Ok " in terminal when I unplugged modem input. But there was no notification either. – user227495 Nov 04 '16 at 04:45
0

There seems to be a nice scripts simple and more sophisticated in answers to similar question at unix stackexchange.

I would personally use something like:

nc -zw1 google.com 80 || notify-send "SAD SAD SAD... There is no connection to google... Go outside and find a real world..."

This will simply send a notify message that will appear in the systray notification manager in case there is no connection to port 80 at google.com. Using the name not the IP of google server will also check DNS resolving.

You can add it in your user cron as job to check each minute for example...