Advertisement
Guest User

50-motd-news

a guest
Feb 18th, 2020
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.67 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. #    50-motd-news - print the live news from the Ubuntu wire
  4. #    Copyright (C) 2016-2017 Canonical Ltd.
  5. #    Copyright (C) 2016-2017 Dustin Kirkland
  6. #
  7. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  8. #
  9. #    This program is free software; you can redistribute it and/or modify
  10. #    it under the terms of the GNU General Public License as published by
  11. #    the Free Software Foundation; either version 2 of the License, or
  12. #    (at your option) any later version.
  13. #
  14. #    This program is distributed in the hope that it will be useful,
  15. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #    GNU General Public License for more details.
  18. #
  19. #    You should have received a copy of the GNU General Public License along
  20. #    with this program; if not, write to the Free Software Foundation, Inc.,
  21. #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22.  
  23. ##############################################################################
  24. # This program could be rewritten in C or Golang for faster performance.
  25. # Or it could be rewritten in Python or another higher level language
  26. # for more modularity.
  27. # However, I've insisted on shell here for transparency!
  28. #                                                                     - Dustin
  29. ##############################################################################
  30.  
  31. # Source the local configuration
  32. [ -r /etc/default/motd-news ] && . /etc/default/motd-news
  33.  
  34. # Exit immediately, unless we're enabled
  35. # This makes this script very easy to disable in /etc/default/motd-news configuration
  36. [ "$ENABLED" = "1" ] || exit 0
  37.  
  38. # Ensure sane defaults
  39. [ -n "$URLS" ] || URLS="https://motd.ubuntu.com"
  40. [ -n "$WAIT" ] || WAIT=5
  41. [ -n "$CACHE" ] || CACHE="/var/cache/motd-news"
  42. [ "$1" = "--force" ] && FORCED=1
  43.  
  44. # Ensure we print safely, maximum of the first 10 lines,
  45. # maximum of the first 80 chars per line, no control chars
  46. safe_print() {
  47.     cat "$1" | head -n 10 | tr -d '\000-\011\013\014\016-\037' | cut -c -80
  48. }
  49.  
  50.  
  51. # If we're not forcing an update, and we have a cached motd-news file,
  52. # then just print it and exit as quickly as possible, for login performance.
  53. # Note that systemd should keep this cache file up to date, asynchronously
  54. if [ "$FORCED" != "1" ]; then
  55.     if [ -r $CACHE ]; then
  56.         echo
  57.         safe_print $CACHE
  58.     else
  59.         : > $CACHE
  60.     fi
  61.     exit 0
  62. fi
  63.  
  64. # If we've made it here, we've been given the --force argument,
  65. # probably from the systemd motd-news.service.  Let's update...
  66.  
  67. # Generate our temp files, clean up when done
  68. NEWS=$(mktemp) || exit 1
  69. ERR=$(mktemp) || exit 1
  70. CLOUD=$(mktemp) || exit 1
  71. trap "rm -f $NEWS $ERR $CLOUD" HUP INT QUIT ILL TRAP KILL BUS TERM
  72.  
  73. # Construct a user agent, similar to Firefox/Chrome/Safari/IE to
  74. # ensure a proper, tailored, accurate message of the day
  75.  
  76. # Curl browser version, for debug purposes
  77. curl_ver="$(dpkg -l curl | awk '$1 == "ii" { print($3); exit(0); }')"
  78.  
  79. # Distribution version, for messages releated to this Ubuntu release
  80. . /etc/lsb-release
  81. lsb=$(echo "$DISTRIB_DESCRIPTION" | sed -e "s/ /\//g")
  82. codename="$DISTRIB_CODENAME"
  83.  
  84. # Kernel version and CPU type, for messages related to a particular revision or hardware
  85. platform="$(uname -o)/$(uname -r)/$(uname -m)"
  86. arch="$(uname -m)"
  87. cpu="$(grep -m1 "^model name" /proc/cpuinfo | sed -e "s/.*: //" -e "s:\s\+:/:g")"
  88. cloud_id="unknown"
  89. if [ -x /usr/bin/cloud-id ]; then
  90.     /usr/bin/cloud-id > "$CLOUD" 2>/dev/null
  91.     if [ $? -eq 0 ]; then
  92.         # sanitize it a bit, just in case
  93.         cloud_id=$(cut -c -40 "${CLOUD}" | tr -c -d '[:alnum:]')
  94.         if [ -z "${cloud_id}" ]; then
  95.             cloud_id="unknown"
  96.         fi
  97.     fi
  98. fi
  99.  
  100. # Some messages may only be pertinent before or after some amount of uptime
  101. read up idle < /proc/uptime
  102. uptime="uptime/$up/$idle"
  103.  
  104. # Piece together the user agent
  105. USER_AGENT="curl/$curl_ver $lsb $platform $cpu $uptime cloud_id/$cloud_id"
  106.  
  107. # Loop over any configured URLs
  108. for u in $URLS; do
  109.     # Ensure https:// protocol, for security reasons
  110.     case $u in
  111.         https://*)
  112.             true
  113.         ;;
  114.         https://motd.ubuntu.com)
  115.             u="$u/$codename/$arch"
  116.         ;;
  117.         *)
  118.             continue
  119.         ;;
  120.     esac
  121.     # If we're forced, set the wait to much higher (1 minute)
  122.     [ "$FORCED" = "1" ] && WAIT=60
  123.     # Fetch and print the news motd
  124.     if curl --connect-timeout "$WAIT" --max-time "$WAIT" -A "$USER_AGENT" -o- "$u" >"$NEWS" 2>"$ERR"; then
  125.         echo
  126.         # At most, 10 lines of text, remove control characters, print at most 80 characters per line
  127.         safe_print "$NEWS"
  128.         # Try to update the cache
  129.         safe_print "$NEWS" 2>/dev/null >$CACHE || true
  130.     else
  131.         : > "$CACHE"
  132.     fi
  133. done
  134. rm -f "$NEWS" "$ERR" "$CLOUD"
  135. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement