Thread SSH Script zur Massenabfrage: kein fehler, kein log, na supi... (20 answers)
Opened by FlorianL at 2007-06-13 13:26

FlorianL
 2007-06-15 13:39
#77489 #77489
User since
2007-05-18
142 Artikel
BenutzerIn
[default_avatar]
danke ;)

Also ich habs dann ma rausbekommen warum der nur zum ersten host connected hat, und zwar musste ich noch das newline für jeden host in der @hostlist entfernen

nuja, es geht alles, aber da ich wieder viel zu viel "fremd-code" von euch drin hab, hab ich nun schwierigkeiten weiter zu kommen, und zwar möchte ich einfach nur das der hostname über dem betreffenden @output erscheint...

Das ist der aktuelle (gut funktionierende) code:

Code (perl): (dl )
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl
#
# Remote-Cmd F.Luettgens
##########################
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi       = "/cgi-bin/remotecmd/remotecmd.cgi";
my $query     = CGI->new;
my @configs   = $query->param;
my %params    = $query->Vars;
my $hostfile  = "/usr/local/etc/rup";

print $query->header();

chomp(my $sshbin = `which ssh`);

open my $hosts, '<', $hostfile or die "Couldnt open $hostfile: $!";
my @hostlist = <$hosts>;
close($hosts);

foreach (@hostlist) {
       s/i$//;
}

my %map = (
       exec_cmd        => \&exec_cmd,
);

my $output  = '';
my $options = join "\n", map{ qq~  <option value="$_">$_</option>~ }@hostlist;
my $goto    = $ENV{PATH_INFO};
   $goto    =~ s!^/!!;

if( exists $map{$goto} ){
    $output = $map{$goto}->(\%params);
    print_output( $output );
}
else{
    print_html( $options );
}

sub exec_cmd {
    my ($paramref) = @_;

    my %params   = %$paramref;
    my $server   = $params{'hostlist'};
    my $command  = $params{'command'};
    my $send2all = $params{'allhosts'};

    my $output   = '';
    if (( $command =~ m/^rm/ ) or ( $command =~ m/^ping/ ) or ( $command =~ m/^login/ ) or ( $command =~ m/^ftp/ ) or ( $command =~ m/^telnet/ ) or ( $command =~ m/^rsh/ )) {
        die "Command $command ist nicht erlaubt!";
    }
    if ($send2all eq "send2all") {
        foreach (@hostlist) {
               chomp;
               $output .= (ssh_connect($_, $command));
        }
    } else {
        chomp $server;
        $output = ssh_connect($server, $command);
    }
    return $output;
}
sub ssh_connect {
    my ($server,$command) = @_;
    my @lines = qx{$sshbin -T $server $command};
    my $output = join "<br />", @lines;
    return $output . "<br />--<br />";
}
sub print_html{
    my ($options) = @_;
    print <<EOT;
<html>
<body>
<form name="remotecmd" method="post" action="./remotecmd.cgi/exec_cmd" target="_down">
<table width="812" border="0" align="center">
<tr>
<td width="94">Hostname</td>
<td width="86"> </td>
<td width="610">Command:</td>
</tr>
<tr>
<td><select name="hostlist" id="hostlist">
$options
</select></td>
<td> </td>
<td><input name="command" type="text" id="command" value="uname -a" size="70"></td>
</tr>
<tr>
<td><input type="radio" name="allhosts" value="send2all">Alle Hosts </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Absenden"></td>
<td><input name="cancel" type="reset" id="cancel" value="Zurücksetzen"></td>
<td> </td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
EOT
}
sub print_output{
    my ($out) = @_;
    print ("$out");
}


Wünsche euch schonmal ein schönes Wochenende :)

View full thread SSH Script zur Massenabfrage: kein fehler, kein log, na supi...