Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1575958
  • 博文数量: 317
  • 博客积分: 10283
  • 博客等级: 上将
  • 技术积分: 3566
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-04 11:38
个人简介

哥使用Linux

文章分类

全部博文(317)

分类: LINUX

2008-02-27 12:27:44

I have cron-apt set up on all my machines — you can get it to install any updates automatically but that sounds like Bad News to me, so instead it’s set to download and email me. I had a script that took names-of-machines-to-upgrade as arguments and did the rest for me, but that involved typing up to 50 machine names. And I am lazy.

So I finally got around to writing a script that parses a local mailbox, grabs the machine names from the subject lines, and does the rest from there. My involvement now is:

  1. Get Thunderbird to show me only the cron-apt emails (via tag filter — tags are automatically applied).
  2. Quick check of the emails to make sure nothing outrageous is going to happen.
  3. Select all, hit Ctrl-6 to move them to the special mailbox ( allows you to allocate up to 10 mailboxes to key combinations).
  4. Find terminal window, run script.

Note that in an ideal world I’d be using Net::SSH::Perl to check for the root ssh key, but I was having problems with CPAN when I wrote this.

 

#!/usr/bin/perl -w


use strict;

my $homedir = "/home/user";
my $file = "$homedir/mail/aptget";
my $sshkey = "$homedir/.ssh/key";
my $cmd = "apt-get -y upgrade";
my @hosts;

sub runcommand();

open FILE,"+<$file";

# Subject line looks like:

# Subject: CRON-APT completed on machinename [/etc/cron-apt/config]

while () {
    next unless /CRON-APT completed/;
    my @line = split;
    my $hostname = $line[4];
    push @hosts, $hostname;
}

# Check if sshkey is in ssh list & add it if not

if (`ssh-add -l` =~ /.* $sshkey/) {
    runcommand();
}
else {
    `ssh-add $sshkey`;
    runcommand();
    `ssh-add -d $sshkey`;
}

print FILE "";
close FILE;


sub runcommand() {
    foreach my $host (@hosts) {
        print "Host is: $host\n";
        system("ssh root\@$host -i $sshkey $cmd");
    }
}

Friday January 18, 2008 6:31AM by in Technical

links:http://www.oreillynet.com/linux/blog/2008/01/automating_debian_updates.html

阅读(2521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~