我希望我的服务器通知我有关未决的安全更新。 但我不想收到有关所有可用更新的通知。 我怎样才能做到这一点?
为了回答我自己的问题:我写了一个小的ruby脚本,它扫描文本文件比包含安全更新信息的login时显示的文件更新:5个更新是安全更新
它甚至可以通过sshlogin到其他机器,并检查该机器以及安全更新。 不过,没有密码的ssh密钥是必需的。 尽可能限制用户的权利是一个好主意。
它的写法尽可能简单,所以即使没有ruby知识,也应该是可以理解的。
#!/usr/bin/env ruby # (c) 2013, Johannes Barre, [email protected] # License: MIT update_file = '/var/lib/update-notifier/updates-available' conf = { mail_to: '[email protected]', servers: { name: :localhost, # name -> Server name for the mail other_server: '[email protected]', # log in to other-server.com using user updatecheck third_server: '[email protected]' } } def get_security_updates(str) str[/([0-9]+) updates are security updates/, 1].to_i end out = {} conf[:servers].each do |name, host| if host == :localhost out[name] = get_security_updates(File.read(update_file)) else out[name] = get_security_updates(`ssh #{host} 'cat #{update_file}'`) end end out.delete_if { |_, v| v == 0 } unless out.empty? IO.popen(%|mail #{conf[:mail_to]} -s "Security updates pending"|, 'r+') { |io| io.print out.map { |host, updates| "#{host} has #{updates} security updates pending" }.join("\n") + "\n" } end
设置一个cron作业,每天运行一次。 我现在运行这个脚本几个月,它工作正常。 如果Debian显示相同的login信息,它也应该在Debian系统上工作。
不是为Ruby 1.9编写的。 我build议升级,因为1.8版本已经没有安全更新了,但是如果你把哈希语法改回旧的样式( :key => 'value'而不是key: 'value' ),它会运行。
与sh脚本类似的解决scheme
#!/bin/sh FLAG=/tmp/updates_available.flag UPDATEFILE=/var/lib/update-notifier/updates-available if [ ! -f ${FLAG} ] then touch ${FLAG} fi diff ${FLAG} ${UPDATEFILE} > /dev/null if [ $? -eq 1 ]; then COUNT=`grep -c "0 updates are security updates" ${UPDATEFILE}` if [ ${COUNT} -eq 0 ]; then sendmessage "`hostname`: `cat ${UPDATEFILE}`" # choose how to send message fi cp ${UPDATEFILE} ${FLAG} fi