5

When I log into an Ubuntu system, I get an informative message like this one:

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.

When I log into the system via the GUI and opening putty terminals, I don't get those messages. Is there a way to automatically display them after login (say, in a special window popping up on the GUI)?

EDIT: The code should be able to handle German language. Testing Serg's code I got the following error message:

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'

obviously caused by the letter "ö" in the German word "können".

1 Answers1

3

Introduction

The information that you see in login to TTY typically is provided by /etc/update-motd.d/. Simplest approach would be to run those scripts and give output to a GUI window, however, some of those scripts require root privilege as I found. The answer below, however, requires no root privilege and provides very simple greeter window.

enter image description here

The code is rough around the edges, probably could use more polish, but it achieves desired effect. I may improve this code eventually to add more features and better look, but for now lets call it release 0.1 :)

Usage

The usage of this program is very simple : python3 greeter_window.py

The command is meant to be added to Startup Applications to appear upon user's login. Please see the related question on how to add a command as Startup Application: https://askubuntu.com/a/48327/295286

Source code

Also available on 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()