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

renee
 2007-06-13 17:37
#77484 #77484
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Das ist jetzt ungetestet:
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
114
115
116
#!/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$//;              &nbsp
;              &nbsp
;   # angehaengtes "i" entfernen
}

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 ($send2all eq "send2all") {               
           # ssh connect an alle rechner?
        foreach (@hostlist) {
               $output .= ssh_connect($_, $command);
        }
    } else {
        $output = ssh_connect($server, $command);              &nbs
p;          # ssh connect
    }

    return $output;
}

sub ssh_connect {
    my ($server,$command) = @_;
    my $output = qx{$sshbin $server $command};
    return $output;
}

sub print_html{
    my ($options) = @_;
}
print <<EOT;
<html>
<body>
 <form name="remotecmd" method="post" action="./remotecmd.cgi/exec_cmd">
 <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&uuml;cksetzen"></td>
 <td> </td>
 </tr>
 </table>
 <p> </p>
 </form>
</body>
</html>
EOT
}

sub print_output{
    my ($out) = @_;
    print $out;
}


Du solltest aber sehr vorsichtig damit sein, dass man die Befehle übermittelt, die auf dem Server abgesetzt werden. Stell Dir vor, einer übermittelt "rm -rf /"
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

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