Skip to content

Instantly share code, notes, and snippets.

@franky47
Last active July 1, 2016 07:49
Show Gist options
  • Save franky47/9908cbfe53abe8764cd96d645495fb2d to your computer and use it in GitHub Desktop.
Save franky47/9908cbfe53abe8764cd96d645495fb2d to your computer and use it in GitHub Desktop.
RasPi 3 Dynamic MOTD à la Ubuntu
#!/bin/bash
#
# 00-header - create the header of the MOTD
# Copyright (c) 2013 Nick Charlton
# Copyright (c) 2009-2010 Canonical Ltd.
#
# Authors: Francois Best <[email protected]>
# Nick Charlton <[email protected]>
# Dustin Kirkland <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Source: https://nickcharlton.net/posts/debian-ubuntu-dynamic-motd.html
# Notes: shell needs to be bash or zsh or other shell with color processing,
# otherwise (like /bin/sh) the escape codes won't print out properly.
# Ascii art for the Raspberry Pi Logo credits to Piksel:
# https://gist.github.com/piksel/3023588
[ -r /etc/lsb-release ] && . /etc/lsb-release
if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
# Fall back to using the very slow lsb_release utility
DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi
logo="\e[32m
.~~. .~~.
'. \ ' ' / .'\e[31m
.~ .~~~..~. \e(B\e[m _ _ ___ \e[31m
: .~.'~'.~. : \e(B\e[m ___ ___ ___ ___| |_ ___ ___ ___ _ _ ___|_| |__ | \e[31m
~ ( ) ( ) ~ \e(B\e[m | _| .'|_ -| . | . | -_| _| _| | | | . | | |_ | \e[31m
( : '~'.~.'~' : ) \e(B\e[m |_| |__,|___| _|___|___|_| |_| |_ | | _|_| |___| \e[31m
~ .~ ( ) ~. ~ \e(B\e[m |_| |___| |_| \e[31m
( : '~' : )
'~ .~~~. ~'
'~'
\e(B\e[m"
printf "$logo"
printf "\n"
printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "$(uname -r)"
printf "\n"
#!/bin/bash
#
# 10-sysinfo - generate the system information
# Copyright (c) 2013 Nick Charlton
#
# Authors: Nick Charlton <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Source: https://nickcharlton.net/posts/debian-ubuntu-dynamic-motd.html
date=`date`
load=`cat /proc/loadavg | awk '{print $1}'`
root_usage=`df -h / | awk '/\// {print $(NF-1)}'`
memory_usage=`free -m | awk '/Mem/ { printf("%3.1f%%", $3/$2*100) }'`
swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", $3/$2*100) }'`
users=`users | wc -w`
echo "System information as of: $date"
echo
printf "System load:\t%s\tMemory usage:\t%s\n" $load $memory_usage
printf "Usage on /:\t%s\tSwap usage:\t%s\n" $root_usage $swap_usage
printf "Local users:\t%s\n" $users
echo
#!/usr/bin/python
#
# 20-updates - create the system updates section of the MOTD
# Copyright (c) 2013 Nick Charlton
#
# Authors: Francois Best <[email protected]>
# Nick Charlton <[email protected]>
# Based upon prior work by Dustin Kirkland and Michael Vogt.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Source: https://nickcharlton.net/posts/debian-ubuntu-dynamic-motd.html
# Modified to detect important packages from RasPi Foundation as "security" updates
# Notes: requires python-apt package to be installed
import sys
import subprocess
import apt_pkg
# DISTRO = subprocess.Popen(["lsb_release", "-c", "-s"],
# stdout=subprocess.PIPE).communicate()[0].strip()
class OpNullProgress(object):
'''apt progress handler which supresses any output.'''
def update(self):
pass
def done(self):
pass
def is_security_upgrade(pkg):
for (file, index) in pkg.file_list:
if file.origin == "Raspberry Pi Foundation" and pkg.priority == 5:
return True
return False
# init apt and config
apt_pkg.init()
# open the apt cache
try:
cache = apt_pkg.Cache(OpNullProgress())
except SystemError, e:
sys.stderr.write("Error: Opening the cache (%s)" % e)
sys.exit(-1)
# setup a DepCache instance to interact with the repo
depcache = apt_pkg.DepCache(cache)
# take into account apt policies
depcache.read_pinfile()
# initialise it
depcache.init()
# give up if packages are broken
if depcache.broken_count > 0:
sys.stderr.write("Error: Broken packages exist.")
sys.exit(-1)
# mark possible packages
try:
# run distro-upgrade
depcache.upgrade(True)
# reset if packages get marked as deleted -> we don't want to break anything
if depcache.del_count > 0:
depcache.init()
# then a standard upgrade
depcache.upgrade()
except SystemError, e:
sys.stderr.write("Error: Couldn't mark the upgrade (%s)" % e)
sys.exit(-1)
# run around the packages
upgrades = 0
security_upgrades = 0
for pkg in cache.packages:
candidate = depcache.get_candidate_ver(pkg)
current = pkg.current_ver
# skip packages not marked as upgraded/installed
if not (depcache.marked_install(pkg) or depcache.marked_upgrade(pkg)):
continue
# increment the upgrade counter
upgrades += 1
# keep another count for security upgrades
if is_security_upgrade(candidate):
security_upgrades += 1
# double check for security upgrades masked by another package
for version in pkg.version_list:
if (current and apt_pkg.version_compare(version.ver_str, current.ver_str) <= 0):
continue
if is_security_upgrade(version):
security_upgrades += 1
break
print "%d updates to install." % upgrades
print "%d are security updates." % security_upgrades
print "" # Trailing line
@franky47
Copy link
Author

franky47 commented Jul 1, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment