en omdat het vrijdag is, krijg je er gratis een script bij (perl):
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| #!/usr/bin/perl -w
# Disable output buffering
$|++;
#
# do.pl Run commands specified in $actions_file
# on all boxen specified in $hostlist_file
#
# TODO:
# - Add os detection (?)
# Use these modules
use Getopt::Std;
use File::Basename;
# Set variables
my $hostlist_file = "hostlist.txt";
my $actions_file = "actions.txt";
my @hostlist = ();
my $actions = "";
my $ssh = "/usr/bin/ssh";
my $ssh_opts = "-C -c blowfish";
my $ping = "/bin/ping";
my $ping_opts = "-c3";
my $ruser = "myuser";
my $do_ping = 0;
my $dry_run = 0;
sub help {
print "Usage: ".basename($0)." [-a <actionfile>] [-l <hostlist>] [-p] [-d] [-h]\n\n";
print " -a <actionfile>\toverride the default actionlist (actions.txt)\n";
print " -l <hostlist>\t\toverride the default hostlist (hostlist.txt)\n";
print " -p\t\t\tPing host. If the host is down, it will be skipped\n";
print " -d\t\t\tDry run, print the actions instead running them\n";
print " -h\t\t\tThis help\n";
}
sub ping {
my $host = shift;
@out = `$ping $ping_opts $host`;
foreach (@out) {
chomp;
my ($tx,$rx,$t) = "";
if (/transmitted/) {
($tx,$t,$t,$rx,$t) = split /\ /;
if ($tx == $rx) { return 1 }
}
}
return undef;
}
# Parse command line parameters
getopt ("al"); getopts ("hd");
if ($opt_h) { undef ($opt_h); help(); exit 1; }
if ($opt_a) { if ( -e $opt_a ) { $actions_file = $opt_a; } else { print "$opt_a does not exist, using default"; } }
if ($opt_l) { if ( -e $opt_l ) { $hostlist_file = $opt_l; } else { print "$opt_l does not exist, using default"; } }
if ($opt_p) { undef ($opt_p); $do_ping = 1; }
if ($opt_d) { undef ($opt_d); $dry_run = 1; }
# Suck in host list
open (HOSTLIST, "$hostlist_file") || die "could not open $hostlist_file: $!";
@hostlist = <HOSTLIST>;
close (HOSTLIST);
# Suck in actions list
open (ACTIONSLIST, "$actions_file") || die "could not open $actions_file: $!";
while ($line = <ACTIONSLIST>) {
chomp ($line);
# To increase efficientcy, paste all commands
# on one line separated with ;'s.
if ($actions ne "") {
$actions = "$actions;$line";
} else {
$actions = $line;
}
}
close (ACTIONSLIST);
# Run $actions for @hostlist
foreach $host (@hostlist) {
chomp ($host);
if ($do_ping == 1) {
print "===> ping $host: ";
if (defined(ping ($host))) {
print "ok\n";
print "===[ $host ]=========================\n";
if ($dry_run == 0 ) {
system ("$ssh $ssh_opts $ruser\@$host \"$actions\" 2>/dev/null");
} else {
print "$ssh $ssh_opts $ruser\@$host \"$actions\" 2>/dev/null\n";
}
} else {
print "timeout\n";
}
}
} |
(sorry voor de layout)
usage is simpel. Zet je hostnames (1 per regel) in hostlist.txt, en de remote commando's in actions.txt (zowel <cr> separated als ";" separated).
[
Voor 0% gewijzigd door
Verwijderd op 18-10-2002 18:12
. Reden: typo ]