Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1864515
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: LINUX

2011-11-16 19:14:58

以前的丢了,重写,备份一下。

Usage: ./distfile.pl [-m machine list file]
        [-l "local path"]
        [-r remote path]
        [-u "user"]
        [-h]
1. machine list file支持“#”做行注释,不必非行首;
2.最好有ssh信任关系;
  1. #!/usr/bin/perl
  2. #ID:icymoon
  3. #Date:2011-10-28
  4. #BugReport:icymoon.cublog.cn
  5. use strict;
  6. use warnings;
  7. use Getopt::Std;

  8. ###############
  9. # Const Values
  10. ###############
  11. ## true and false, success and fail
  12. use constant true => 1;
  13. use constant TRUE => 1;
  14. use constant false => 0;
  15. use constant FALSE =>0;

  16. my $uls = "\e[4m";
  17. my $ule = "\e[0m";
  18. my $reds = "\e[31;1m";
  19. my $greens = "\e[32;1m";
  20. my $colore = "\e[0m";

  21. ####################
  22. # System Commands
  23. ####################
  24. my $SSH_CMD="/usr/bin/ssh -o ConnectTimeout=3 ";
  25. my $SCP_CMD="/usr/bin/scp -o ConnectTimeout=3 -r";
  26. my $CD_CMD="cd ";

  27. ####################
  28. # Global Vars
  29. ####################
  30. my %options;
  31. my @hosts;
  32. my $localpath;
  33. my $remote_dir;
  34. my $user;
  35. my $scpcmdhead;
  36. my $scpcmdtail;
  37. my $scpcmdgen=false;
  38. my %failedlist = ();

  39. my %opt_err_num_msg =
  40. (
  41.  '99'=>"Machine list file needed",
  42.  '98'=>"Invalied format of Machine list file",
  43.  '97'=>"Can't open machine list file",
  44.  '96'=>"No valid host to run command or script",
  45.  '95'=>"Invalid host",
  46.  '89'=>"Command or Script needed",
  47.  '88'=>"Invalied Script"
  48.  );

  49. ##################
  50. # Basic functions
  51. ##################
  52. # print usage info and quit
  53. # $_[0] program name
  54. sub usage($)
  55. {
  56.     print("Usage: $0 [-m ${uls}machine list file$ule]\n");
  57.     print(" [-l ${uls}\"local path\"$ule]\n");
  58.     print(" [-r ${uls}remote path$ule]\n");
  59.     print(" [-u ${uls}\"user\"$ule]\n");
  60.     print(" [-h]\n");
  61.     exit(0);
  62. }

  63. # print error message and exit with exit code
  64. # $_[0] error message
  65. # $_[1] exit code
  66. sub err_exit($$)
  67. {
  68.     print "$_[0]\n";
  69.     exit $_[1];
  70. }

  71. sub init_hosts($) {
  72.     my $hosts_file = $_[0];
  73.     my @tmp;
  74.     if(-f $hosts_file) {
  75.         if(open(FD,"<$hosts_file")) {
  76.             my $line;
  77.             while($line=<FD>) {
  78.                 chomp($line);
  79.                 if($line =~ /^\s*#/) {
  80.                     next;
  81.                 }
  82.                 @tmp = split("#", $line);
  83.                 if($#tmp >= 0 ) {
  84.                     if($tmp[0] =~ /[\.a-z0-9A-Z\_\-]+/) {
  85.                         push(@hosts, $tmp[0]);
  86.                     } else {
  87.                         err_exit("$opt_err_num_msg{'95'}: @tmp" , 95);
  88.                     }
  89.                 }
  90.                 @tmp = ();
  91.             }
  92.         } else {
  93.             err_exit("$opt_err_num_msg{'97'}" , 97);
  94.         }
  95.     } else {
  96.         err_exit("$opt_err_num_msg{'98'}" , 98);
  97.     }
  98.     if($#hosts < 0) {
  99.         err_exit("$opt_err_num_msg{'96'}" , 96);
  100.     }
  101. }

  102. sub check_opts() {
  103.     my $run = false;
  104.     if($#ARGV < 0 or $ARGV[0] !~ /^\-/ or $ARGV[0] =~ /^\-\-/) {
  105.         usage($0);
  106.     }
  107.     getopts("m:l:r:u:h",\%options);
  108.     if(defined($options{h})) {
  109.         usage($0);
  110.     }
  111.     if(defined($options{m}) and -f $options{m}) {
  112.         init_hosts($options{m});
  113.     } else {
  114.         err_exit("$opt_err_num_msg{'99'}" , 99);
  115.     }
  116.     if(defined($options{l})) {
  117.         $localpath=$options{l};
  118.         if(! -f $localpath and ! -d $localpath) {
  119.             err_exit("$opt_err_num_msg{'88'}" , 88);
  120.         }
  121.         $run = true;
  122.     }
  123.     if(defined($options{r})) {
  124.         $remote_dir=$options{r};
  125.     }

  126.     if(defined($options{u})) {
  127.         $user=$options{u};
  128.     }
  129.     if(!$run) {
  130.         err_exit("$opt_err_num_msg{'89'}" , 89);
  131.     }
  132. }

  133. sub gen_scpcommand() {
  134.     if($scpcmdgen) {
  135.         return;
  136.     }
  137.     if(defined($user)) {
  138.         $scpcmdhead="$SCP_CMD $localpath $user@";
  139.     } else {
  140.         $scpcmdhead="$SCP_CMD $localpath ";
  141.     }
  142.     if(defined($remote_dir)) {
  143.         $scpcmdtail=":\"$remote_dir\"";
  144.     } else {
  145.         $scpcmdtail=":\"~\/\"";
  146.     }
  147.     $scpcmdgen = true;
  148. }

  149. sub run_scp($) {
  150.     my $ret;
  151.     gen_scpcommand();
  152.     my $h = $_[0];
  153.     my $scpcmd = "${scpcmdhead}${h}$scpcmdtail";
  154.     print("$scpcmd\n");
  155.     $ret = `$scpcmd`;
  156.     my $rv = $?>>8;
  157.     if($rv ne 0) {
  158.         print("${reds}$h:Distribute file $localpath fail[ret=$rv]$colore\n");
  159.         $failedlist{$h} = $rv;
  160.         return;
  161.     } else {
  162.         print("${greens}$h:Success$colore\n");
  163.     }
  164.     print $ret;
  165. }

  166. ##################
  167. # Main process
  168. ##################
  169. check_opts();
  170. foreach my $h (@hosts) {
  171.     run_scp($h);
  172. }

  173. if (%failedlist) {
  174.     print("==============Failed Hosts===================\n");
  175.     foreach my $f (keys(%failedlist)) {
  176.         print("$f:$failedlist{$f}\n");
  177.     }
  178. } else {
  179.     print("All Successed, it seems.\n");
  180. }

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