AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 847754
Accepted
jk - Reinstate Monica
jk - Reinstate Monica
Asked: 2016-11-11 03:04:18 +0800 CST2016-11-11 03:04:18 +0800 CST 2016-11-11 03:04:18 +0800 CST

使用 GUI 登录显示控制台消息

  • 772

当我登录 Ubuntu 系统时,我会收到一条信息性消息,如下所示:

Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-34-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Thu Nov 10 11:17:53 CET 2016

  System load:  0.0                 Processes:           128
  Usage of /:   75.1% of 680.78GB   Users logged in:     1
  Memory usage: 37%                 IP address for eth0: xxx.xxx.xxx.xxx
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

37 packages can be updated.
24 updates are security updates.

New release '16.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2019.

当我通过 GUI 登录系统并打开腻子终端时,我没有收到这些消息。有没有办法在登录后自动显示它们(例如,在 GUI 上弹出的特殊窗口中)?

编辑:代码应该能够处理德语。测试 Serg 的代码我收到以下错误消息:

E: Unbekannter Fehler: \xbb<class 'UnicodeEncodeError'>\xab ('ascii' 
codec can't encode character '\xf6' in position 20: ordinal not in 
range(128))Traceback (most recent call last):
  File "./greeter_window.py", line 92, in <module>
    win = GreeterWindow()
  File "./greeter_window.py", line 29, in __init__
    lines.append('\n\n' + self.get_updates())
  File "./greeter_window.py", line 41, in get_updates
    return self.run_cmd(cmd).decode().strip()
AttributeError: 'NoneType' object has no attribute 'decode'

显然是由德语单词“können”中的字母“ö”引起的。

login
  • 1 1 个回答
  • 1204 Views

1 个回答

  • Voted
  1. Best Answer
    Sergiy Kolodyazhnyy
    2016-11-26T15:02:40+08:002016-11-26T15:02:40+08:00

    介绍

    您在登录 TTY 时看到的信息通常由/etc/update-motd.d/. 最简单的方法是运行这些脚本并将输出输出到 GUI 窗口,但是,我发现其中一些脚本需要 root 权限。然而,下面的答案不需要 root 权限,并提供非常简单的欢迎窗口。

    在此处输入图像描述

    代码的边缘很粗糙,可能可以使用更多的润色,但它达到了预期的效果。我最终可能会改进此代码以添加更多功能和更好的外观,但现在我们将其称为版本 0.1 :)

    用法

    这个程序的使用非常简单:python3 greeter_window.py

    该命令旨在添加到启动应用程序以在用户登录时出现。请参阅有关如何将命令添加为启动应用程序的相关问题:https ://askubuntu.com/a/48327/295286

    源代码

    也可以在GitHub上找到

    #!/usr/bin/env python3
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk,Gdk
    import subprocess
    import psutil
    import os
    
    class  GreeterWindow(Gtk.Window):
    
        def __init__(self):
            Gtk.Window.__init__(self,title="Welcome")
            name = self.get_os_name()
            grid = Gtk.Grid()
            grid.set_border_width(25)
            self.add(grid)
    
            greeting = Gtk.Label("Welcome to "+name+"!\n\n")
            grid.add(greeting)
    
            sysinfo = [ str(i) for i in self.get_system_info()]
            fields =  ['Load Avg:','Memory %:','Swap %:',
                                '/ usage %:','Process count:','User count:'
            ]
            lines = [ fields[i] + " " + sysinfo[i] for i in range(len(sysinfo))]
    
            lines.append( '\nIP addresses:\n' + self.get_ip_addresses()  )
            lines.append('\n\n' + self.get_updates())
    
            label1 = Gtk.Label("\n".join(lines))
    
            grid.attach_next_to(label1,greeting,Gtk.PositionType.BOTTOM,1,2)
            #grid.add(label1)
            button = Gtk.Button(label="Got it !")
            button.connect("clicked", self.on_button_clicked)
            grid.attach_next_to(button,label1,Gtk.PositionType.BOTTOM,1,2)
    
        def get_updates(self,*args):
            cmd = "/usr/lib/update-notifier/apt-check --human-readable".split()
            return self.run_cmd(cmd).decode().strip()
    
        def on_button_clicked(self,*args):
            Gtk.main_quit()
    
        def get_ip_addresses(self,*args):
            cmd = ['ip','-o','addr','show']
            result = self.run_cmd(cmd)
            ipaddr = ipaddr_str = None
    
            if result:
                ipaddr = [ (i.split()[1],i.split()[3])
                           for i in result.decode().strip().split('\n')
    
                ]
                ipaddr_str = "\n".join([str(i[0]) + " " + str(i[1])
                                        for i in ipaddr
                ])
            return ipaddr_str
    
        def get_os_name(self,*args):
            with open('/etc/os-release') as f:
                 for line in f:
                     if line.startswith('PRETTY_NAME'):
                         return line.split('=')[1].replace('"','').strip()
    
        def run_cmd(self, cmdlist):
            """ utility: reusable function for running external commands """
            try:
                stdout = subprocess.check_output(cmdlist)
            except subprocess.CalledProcessError:
                pass
            else:
                if stdout:
                    return stdout
    
    
        def get_system_info(self,*args):
            load = os.getloadavg()
            virtmem = psutil.virtual_memory().percent
            swapmem = psutil.swap_memory().percent
            disk_usage = psutil.disk_usage('/').percent
            num_procs = len(psutil.pids())
            user_count = len(set([ i.name for i in  psutil.users()]))
            return [load,virtmem,swapmem,
                    disk_usage,num_procs,user_count
            ]
    
    
    win = GreeterWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
    #win.override_background_color(Gtk.StateType.NORMAL, Gdk    .RGBA(225,225,0,1))
    win.resize(350,350)
    win.show_all()
    Gtk.main()
    
    • 3

相关问题

  • 在登录屏幕上自动选择用户

  • 在哪里可以找到适用于 Ubuntu 的 Novell NetWare 客户端?

  • gdm登录屏幕中的名称顺序存储在哪里?[复制]

  • xubuntu 中没有其他用户的面板

  • 登录失败并显示低图形然后崩溃?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve