Unity 16.04 版中连接通知受限或无连接通知

Unity 16.04 版中连接通知受限或无连接通知

Unity 16.04 版缺少这个功能,这让我很头疼。如果 WiFi 在路由器级别处于活动状态,但当不能访问网络(我的 ISP 有时会出现故障),我无法在 Unity 16.04 中了解情况。即使是 2009 年推出的 Windows 7 也会在“无互联网访问”时发出通知。

还有其他文件管理器或 DE 可供我使用吗?或者,有针对 16.04 Unity 的修改吗?谢谢。

答案1

最简单的方法是用它ping -c4 google.com来测试你的互联网连接。如果它可以越过路由器到达任何网址,那么你就可以访问互联网了。这可以很容易地适应一个脚本,定期 ping 你所选择的站点。但我有一个稍微不同的想法。

这是顶部面板指示器,它会定期请求互联网连接。如果您的互联网连接中断,它的图标将变为警告标志。它使用标准图标名称,因此无需添加任何其他图标。

将其另存为文件,确保它具有可执行权限chmod +x interwebs-indicator(从终端)或通过文件管理器中的右键单击菜单。在终端中手动运行

python3 intwerwebs-indicator

或者

./interwebs-indicator

如果已授予其可执行权限。

你可以做到在 GUI 登录时自动启动,也。

简单易用。

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

#
# Author: Serg Kolo , contact: [email protected]
# 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()

答案2

似乎有一个很好的脚本简单和更复杂的答案类似的问题unix stackexchange

我个人会使用类似的东西:

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

如果未连接到 google.com 的 80 端口,这将仅发送一条通知消息,该消息将出现在系统托盘通知管理器中。使用名称(而不是 google 服务器的 IP)也将检查 DNS 解析。

您可以将其添加到您的用户 cron 中作为作业来检查每分钟,例如...

相关内容