以前的丢了,重写,备份一下。
Usage: ./distfile.pl [-m machine list file]
[-l "local path"]
[-r remote path]
[-u "user"]
[-h]
1. machine list file支持“#”做行注释,不必非行首;
2.最好有ssh信任关系;
- #!/usr/bin/perl
-
#ID:icymoon
-
#Date:2011-10-28
-
#BugReport:icymoon.cublog.cn
-
use strict;
-
use warnings;
-
use Getopt::Std;
-
-
###############
-
# Const Values
-
###############
-
## true and false, success and fail
-
use constant true => 1;
-
use constant TRUE => 1;
-
use constant false => 0;
-
use constant FALSE =>0;
-
-
my $uls = "\e[4m";
-
my $ule = "\e[0m";
-
my $reds = "\e[31;1m";
-
my $greens = "\e[32;1m";
-
my $colore = "\e[0m";
-
-
####################
-
# System Commands
-
####################
-
my $SSH_CMD="/usr/bin/ssh -o ConnectTimeout=3 ";
-
my $SCP_CMD="/usr/bin/scp -o ConnectTimeout=3 -r";
-
my $CD_CMD="cd ";
-
-
####################
-
# Global Vars
-
####################
-
my %options;
-
my @hosts;
-
my $localpath;
-
my $remote_dir;
-
my $user;
-
my $scpcmdhead;
-
my $scpcmdtail;
-
my $scpcmdgen=false;
-
my %failedlist = ();
-
-
my %opt_err_num_msg =
-
(
-
'99'=>"Machine list file needed",
-
'98'=>"Invalied format of Machine list file",
-
'97'=>"Can't open machine list file",
-
'96'=>"No valid host to run command or script",
-
'95'=>"Invalid host",
-
'89'=>"Command or Script needed",
-
'88'=>"Invalied Script"
-
);
-
-
##################
-
# Basic functions
-
##################
-
# print usage info and quit
-
# $_[0] program name
-
sub usage($)
-
{
-
print("Usage: $0 [-m ${uls}machine list file$ule]\n");
-
print(" [-l ${uls}\"local path\"$ule]\n");
-
print(" [-r ${uls}remote path$ule]\n");
-
print(" [-u ${uls}\"user\"$ule]\n");
-
print(" [-h]\n");
-
exit(0);
-
}
-
-
# print error message and exit with exit code
-
# $_[0] error message
-
# $_[1] exit code
-
sub err_exit($$)
-
{
-
print "$_[0]\n";
-
exit $_[1];
-
}
-
-
sub init_hosts($) {
-
my $hosts_file = $_[0];
-
my @tmp;
-
if(-f $hosts_file) {
-
if(open(FD,"<$hosts_file")) {
-
my $line;
-
while($line=<FD>) {
-
chomp($line);
-
if($line =~ /^\s*#/) {
-
next;
-
}
-
@tmp = split("#", $line);
-
if($#tmp >= 0 ) {
-
if($tmp[0] =~ /[\.a-z0-9A-Z\_\-]+/) {
-
push(@hosts, $tmp[0]);
-
} else {
-
err_exit("$opt_err_num_msg{'95'}: @tmp" , 95);
-
}
-
}
-
@tmp = ();
-
}
-
} else {
-
err_exit("$opt_err_num_msg{'97'}" , 97);
-
}
-
} else {
-
err_exit("$opt_err_num_msg{'98'}" , 98);
-
}
-
if($#hosts < 0) {
-
err_exit("$opt_err_num_msg{'96'}" , 96);
-
}
-
}
-
-
sub check_opts() {
-
my $run = false;
-
if($#ARGV < 0 or $ARGV[0] !~ /^\-/ or $ARGV[0] =~ /^\-\-/) {
-
usage($0);
-
}
-
getopts("m:l:r:u:h",\%options);
-
if(defined($options{h})) {
-
usage($0);
-
}
-
if(defined($options{m}) and -f $options{m}) {
-
init_hosts($options{m});
-
} else {
-
err_exit("$opt_err_num_msg{'99'}" , 99);
-
}
-
if(defined($options{l})) {
-
$localpath=$options{l};
-
if(! -f $localpath and ! -d $localpath) {
-
err_exit("$opt_err_num_msg{'88'}" , 88);
-
}
-
$run = true;
-
}
-
if(defined($options{r})) {
-
$remote_dir=$options{r};
-
}
-
-
if(defined($options{u})) {
-
$user=$options{u};
-
}
-
if(!$run) {
-
err_exit("$opt_err_num_msg{'89'}" , 89);
-
}
-
}
-
-
sub gen_scpcommand() {
-
if($scpcmdgen) {
-
return;
-
}
-
if(defined($user)) {
-
$scpcmdhead="$SCP_CMD $localpath $user@";
-
} else {
-
$scpcmdhead="$SCP_CMD $localpath ";
-
}
-
if(defined($remote_dir)) {
-
$scpcmdtail=":\"$remote_dir\"";
-
} else {
-
$scpcmdtail=":\"~\/\"";
-
}
-
$scpcmdgen = true;
-
}
-
-
sub run_scp($) {
-
my $ret;
-
gen_scpcommand();
-
my $h = $_[0];
-
my $scpcmd = "${scpcmdhead}${h}$scpcmdtail";
-
print("$scpcmd\n");
-
$ret = `$scpcmd`;
-
my $rv = $?>>8;
-
if($rv ne 0) {
-
print("${reds}$h:Distribute file $localpath fail[ret=$rv]$colore\n");
-
$failedlist{$h} = $rv;
-
return;
-
} else {
-
print("${greens}$h:Success$colore\n");
-
}
-
print $ret;
-
}
-
-
##################
-
# Main process
-
##################
-
check_opts();
-
foreach my $h (@hosts) {
-
run_scp($h);
-
}
-
-
if (%failedlist) {
-
print("==============Failed Hosts===================\n");
-
foreach my $f (keys(%failedlist)) {
-
print("$f:$failedlist{$f}\n");
-
}
-
} else {
-
print("All Successed, it seems.\n");
-
}
阅读(2926) | 评论(0) | 转发(0) |