2012-10-02

大量修改Crontab Job

當修改多台機器或多個使用者的crontab時,若時間充裕的狀態下,我們可能一台一台主機登入並一一對每個使用者的crontab去做修改,但同時要改9x台主機或多個使用者的話,應該不會想用徒手煉鋼的方式去完成此任務,因此透過每個使用者已預先埋好的ssh key,我們可透過下列這支Script順利完成:

P.S.前提是至少要有三個配置檔,crontab_ssh_host、crontab_ssh_user和crontab_user1,配置檔內容如底端範例所示。



#!/usr/bin/perl -w
use strict;
##################################################
## Target: Deploy cron Job to each host and user##
## ##
## Date:   2012/10/01 ##
## Ver:    1.0 ##
## Author: Jammy Yu ##
##################################################

## Initial setup
my $program_dir = '/tmp/deploy_cronjob';
my $cron_dir = '/var/spool/cron/crontabs';
my @host;
my @user;
my @user_crontab;

## Load host name
open(HOST, "$program_dir/crontab_ssh_host.txt") || die "ERROR:Can't open host file\n";
while ( <HOST> )
{
   chomp;
   next if /^\s+/;
   next if /^#/;
   push(@host, $_);
}
close(HOST);

## Load user name
open(USER, "$program_dir/crontab_ssh_user.txt") || die "ERROR:Can't open user file\n";
while ( <USER> )
{
   chomp;
   next if /^\s+/;
   next if /^#/;
   push(@user, $_);
}
close(USER);

## Calculate the number of user's crontab job
opendir(DIR, "$program_dir") || die "ERROR:Can't open program dir\n";
while( defined( my $filename = readdir(DIR) ) )
{
   next if ($filename eq '.' || $filename eq '..');
   next if ($filename eq 'crontab_ssh.pl');
   next if ($filename eq 'crontab_ssh1.pl');
   next if ($filename eq 'crontab_ssh_host.txt');
   next if ($filename eq 'crontab_ssh_user.txt');
   push(@user_crontab, $filename);
}
close(DIR);

##################################################
## Deploy the crontab job task to each host ##
##################################################
if (@user_crontab == @user)
{
   foreach my $host (@host)
   {
      scp_key($host);
     
      foreach my $user (@user)
      {
         `/usr/bin/scp $program_dir/crontab_$user root@"$host":$cron_dir/$user`;
         `ssh root@"$host" chown root:cron $cron_dir/$user`;
         `ssh root@"$host" chmod 600 $cron_dir/$user`;
      }
   }
}
else
{
   print "ERROR:defined users from crontab_ssh_user.txt and defined crontab_\$user counters are not the same\n";
   exit 1;
}

sub scp_key
{
   my $target_host = shift;
   my $tmp_dir = '/tmp';
   my $command;
   $command = "/usr/bin/cat ~/.ssh/id_rsa.pub > $tmp_dir/key";
   `LANG=C $command`;
   $command = "/usr/bin/scp -q $tmp_dir/key root\@$target_host:~/.ssh/authorized_keys2";
   `LANG=C $command`;
   $command = "/usr/bin/rm $tmp_dir/key 2>/dev/null";
   `LANG=C $command`;
 
   return($target_host);
}

================================================================
crontab_ssh_host檔案內容格式:
## Insert the host name or IP address
10.199.131.238
10.199.131.239
================================================================
crontab_ssh_user檔案內容格式:
## Insert the user name 
tommy
jammy
================================================================
crontab_user1檔案內容格式:
45 2 * * 0 /usr/lib/spell/compress
45 23 * * * ulimit 5000; /usr/lib/smdemon.cleanu > /dev/null 
================================================================
crontab_user2檔案內容格式:
45 2 * * 0 /usr/lib/spell/compress
================================================================