Toon posts:

hoe commando uitvoeren op andere pc via script.

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik heb 2 servers, nu draait er op 1 van die 2 een script die data uit een sql databeest haald, en aan de hand daarvan bepaalde commando's uitvoerd. Dat werkt allemaal goed.

Allen wil ik met dat script ook commando's uitvoeren op de andere pc (commando's waar root priveleges voor nodig zijn). Ik ben al een beetje wezen spelen met telnet, maar ik vraag me af of er geen betere methode is.

  • Niek
  • Registratie: Februari 2001
  • Laatst online: 13-05 13:40

Niek

f.k.a. The_Surfer

SSH misschien :? www.openssh.com

À vaincre sans péril, on triomphe sans gloire - Pierre Corneille


Verwijderd

Topicstarter
kan ik dat makkelijk in een (bash) script verwerken???

Ik zoek eigenlijk iets waarmee ik die commando's direct naar het systeem kan sturen(de 2 pc's zijn met 2 netwerken verbonden, 1 onderling (crosscable) voor de nfs, en waar ook de commando's overheen moetten, en 1 op het openbaar netwerk (wat eerst door ipchains heen moet).

De beveiliging is dus niet het probleem.

Verwijderd

rsh

Verwijderd

1) vergeet rsh, om de bekende redenen
2) gebruik ssh ism: een pub/priv key en keychain oid.

ssh -C -c blowfish -i /path/to/priv/key <user>@<host> "<command1> ; <command2>"

  • Wilke
  • Registratie: December 2000
  • Laatst online: 08:17
Ik sluit mij geheel en al aan bij r3b00t.

Voor een beschrijving van hoe dat gedonder met keychain e.d. werkt, zie de Gentoo homepage, daar staan aan de linkerkant links naar een prima beschrijving hoe dat moet.

Verwijderd

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 ]


Verwijderd

Verwijderd schreef op 18 oktober 2002 @ 17:31:
1) vergeet rsh, om de bekende redenen
Op een lokaal (trusted) netwerk is rsh veel interessanter, omdat je dan geen CPU aan decryptie/encryptie hoeft te verspelen en er meer CPU voor het commando overblijft... In ons videoproject gebruiken we rsh voor distributed encoding, dus de ene computer doet JPEG decoding, de andere denoising en de derde MPEG encoden (bv.). Als je al die video data tussen de computer streamt met encryptie, dan ben je echt minstens twee keer zo lang bezig!

In sommige gevallen is rsh echt interessant!
Pagina: 1