Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # install figlet to enable ASCII art
  4. apt-get install figlet
  5. # create directory
  6. mkdir /etc/update-motd.d/
  7. # change to new directory
  8. cd /etc/update-motd.d/
  9. # create dynamic files
  10. touch 00-header && touch 10-sysinfo && touch 20-updates && touch 90-footer
  11. # make files executable
  12. chmod +x /etc/update-motd.d/*
  13. # remove MOTD file
  14. rm /etc/motd
  15. # symlink dynamic MOTD file
  16. ln -s /var/run/motd /etc/motd
  17.  
  18. cat <<EOF >> 00-header
  19. #!/bin/sh
  20. #
  21. # 00-header - create the header of the MOTD
  22. # Copyright (c) 2013 Nick Charlton
  23. # Copyright (c) 2009-2010 Canonical Ltd.
  24. #
  25. # Authors: Nick Charlton <hello@nickcharlton.net>
  26. # Dustin Kirkland <kirkland@canonical.com>
  27. #
  28. # This program is free software; you can redistribute it and/or modify
  29. # it under the terms of the GNU General Public License as published by
  30. # the Free Software Foundation; either version 2 of the License, or
  31. # (at your option) any later version.
  32. #
  33. # This program is distributed in the hope that it will be useful,
  34. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. # GNU General Public License for more details.
  37. #
  38. # You should have received a copy of the GNU General Public License along
  39. # with this program; if not, write to the Free Software Foundation, Inc.,
  40. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  41.  
  42. [ -r /etc/lsb-release ] && . /etc/lsb-release
  43.  
  44. if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
  45. # Fall back to using the very slow lsb_release utility
  46. DISTRIB_DESCRIPTION=$(lsb_release -s -d)
  47. fi
  48.  
  49. figlet $(hostname)
  50. printf "\n"
  51.  
  52. printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "$(uname -r)"
  53. printf "\n"
  54. EOF
  55.  
  56. cat <<EOF >> 10-sysinfo
  57. #!/bin/bash
  58. #
  59. # 10-sysinfo - generate the system information
  60. # Copyright (c) 2013 Nick Charlton
  61. #
  62. # Authors: Nick Charlton <hello@nickcharlton.net>
  63. #
  64. # This program is free software; you can redistribute it and/or modify
  65. # it under the terms of the GNU General Public License as published by
  66. # the Free Software Foundation; either version 2 of the License, or
  67. # (at your option) any later version.
  68. #
  69. # This program is distributed in the hope that it will be useful,
  70. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  71. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  72. # GNU General Public License for more details.
  73. #
  74. # You should have received a copy of the GNU General Public License along
  75. # with this program; if not, write to the Free Software Foundation, Inc.,
  76. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  77.  
  78. date=`date`
  79. load=`cat /proc/loadavg | awk '{print $1}'`
  80. root_usage=`df -h / | awk '/\// {print $(NF-1)}'`
  81. memory_usage=`free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { printf("%3.1f%%", used/total*100)}'`
  82. swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", "exit !$2;$3/$2*100") }'`
  83. users=`users | wc -w`
  84. time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'`
  85. processes=`ps aux | wc -l`
  86. ip=`ifconfig $(route | grep default | awk '{ print $8 }') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
  87.  
  88. echo "System information as of: $date"
  89. echo
  90. printf "System load:\t%s\tIP Address:\t%s\n" $load $ip
  91. printf "Memory usage:\t%s\tSystem uptime:\t%s\n" $memory_usage "$time"
  92. printf "Usage on /:\t%s\tSwap usage:\t%s\n" $root_usage $swap_usage
  93. printf "Local Users:\t%s\tProcesses:\t%s\n" $users $processes
  94. echo
  95. EOF
  96.  
  97. cat <<EOF >> 20-updates
  98. #!/usr/bin/python
  99. #
  100. # 20-updates - create the system updates section of the MOTD
  101. # Copyright (c) 2013 Nick Charlton
  102. #
  103. # Authors: Nick Charlton <hello@nickcharlton.net>
  104. # Based upon prior work by Dustin Kirkland and Michael Vogt.
  105. #
  106. # This program is free software; you can redistribute it and/or modify
  107. # it under the terms of the GNU General Public License as published by
  108. # the Free Software Foundation; either version 2 of the License, or
  109. # (at your option) any later version.
  110. #
  111. # This program is distributed in the hope that it will be useful,
  112. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  113. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  114. # GNU General Public License for more details.
  115. #
  116. # You should have received a copy of the GNU General Public License along
  117. # with this program; if not, write to the Free Software Foundation, Inc.,
  118. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  119.  
  120. import sys
  121. import subprocess
  122. import apt_pkg
  123.  
  124. DISTRO = subprocess.Popen(["lsb_release", "-c", "-s"],
  125. stdout=subprocess.PIPE).communicate()[0].strip()
  126.  
  127. class OpNullProgress(object):
  128. '''apt progress handler which supresses any output.'''
  129. def update(self):
  130. pass
  131. def done(self):
  132. pass
  133.  
  134. def is_security_upgrade(pkg):
  135. '''
  136. Checks to see if a package comes from a DISTRO-security source.
  137. '''
  138. security_package_sources = [("Ubuntu", "%s-security" % DISTRO),
  139. ("Debian", "%s-security" % DISTRO)]
  140.  
  141. for (file, index) in pkg.file_list:
  142. for origin, archive in security_package_sources:
  143. if (file.archive == archive and file.origin == origin):
  144. return True
  145. return False
  146.  
  147. # init apt and config
  148. apt_pkg.init()
  149.  
  150. # open the apt cache
  151. try:
  152. cache = apt_pkg.Cache(OpNullProgress())
  153. except SystemError, e:
  154. sys.stderr.write("Error: Opening the cache (%s)" % e)
  155. sys.exit(-1)
  156.  
  157. # setup a DepCache instance to interact with the repo
  158. depcache = apt_pkg.DepCache(cache)
  159.  
  160. # take into account apt policies
  161. depcache.read_pinfile()
  162.  
  163. # initialise it
  164. depcache.init()
  165.  
  166. # give up if packages are broken
  167. if depcache.broken_count > 0:
  168. sys.stderr.write("Error: Broken packages exist.")
  169. sys.exit(-1)
  170.  
  171. # mark possible packages
  172. try:
  173. # run distro-upgrade
  174. depcache.upgrade(True)
  175. # reset if packages get marked as deleted -> we don't want to break anything
  176. if depcache.del_count > 0:
  177. depcache.init()
  178.  
  179. # then a standard upgrade
  180. depcache.upgrade()
  181. except SystemError, e:
  182. sys.stderr.write("Error: Couldn't mark the upgrade (%s)" % e)
  183. sys.exit(-1)
  184.  
  185. # run around the packages
  186. upgrades = 0
  187. security_upgrades = 0
  188. for pkg in cache.packages:
  189. candidate = depcache.get_candidate_ver(pkg)
  190. current = pkg.current_ver
  191.  
  192. # skip packages not marked as upgraded/installed
  193. if not (depcache.marked_install(pkg) or depcache.marked_upgrade(pkg)):
  194. continue
  195.  
  196. # increment the upgrade counter
  197. upgrades += 1
  198.  
  199. # keep another count for security upgrades
  200. if is_security_upgrade(candidate):
  201. security_upgrades += 1
  202.  
  203. # double check for security upgrades masked by another package
  204. for version in pkg.version_list:
  205. if (current and apt_pkg.version_compare(version.ver_str, current.ver_str) <= 0):
  206. continue
  207. if is_security_upgrade(version):
  208. security_upgrades += 1
  209. break
  210.  
  211. print "%d updates to install." % upgrades
  212. print "%d are security updates." % security_upgrades
  213. print "" # leave a trailing blank line
  214. EOF
  215.  
  216. cat <<EOF >> 90-footer
  217. #!/bin/sh
  218. #
  219. # 90-footer - write the admin's footer to the MOTD
  220. # Copyright (c) 2013 Nick Charlton
  221. # Copyright (c) 2009-2010 Canonical Ltd.
  222. #
  223. # Authors: Nick Charlton <hello@nickcharlton.net>
  224. # Dustin Kirkland <kirkland@canonical.com>
  225. #
  226. # This program is free software; you can redistribute it and/or modify
  227. # it under the terms of the GNU General Public License as published by
  228. # the Free Software Foundation; either version 2 of the License, or
  229. # (at your option) any later version.
  230. #
  231. # This program is distributed in the hope that it will be useful,
  232. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  233. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  234. # GNU General Public License for more details.
  235. #
  236. # You should have received a copy of the GNU General Public License along
  237. # with this program; if not, write to the Free Software Foundation, Inc.,
  238. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  239.  
  240. [ -f /etc/motd.tail ] && cat /etc/motd.tail || true
  241. EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement