Perl: exemple SSH avec Expect
Example illustrant l'utilisation d'Expect + SSH:
1 #!/bin/env perl
2 use Expect;
3 my $fileTraceExpect = "/tmp/dummy.trace";
4 my $userlogin="root";
5 my $userpw="mypasswd";
6 my $remotehost="192.168.0.2";
7 #
8 # Start a sftp connection
9 my $exp = new Expect;
10 #
11 # save results of expect and send commands
12 $exp->log_file($fileTraceExpect, "w");
13 # no print on stdout
14 $exp->log_stdout(0);
15 #
16 my $timeout = 2;
17 #
18 # open ssh connection
19 my $command = "ssh ".$userlogin."@".$remotehost."\\n";
20 my $ssh= $exp->spawn($command) || die "Cannot spawn $command: $!\\n";
21 #
22 if ($ssh->expect($timeout, "Are you sure you want to continue connecting")) {
23 $ssh->send("yes\\r");
24 $ssh->expect($timeout, "Password:");
25 }
26 #
27 $ssh->send("$userpw\\n");
28 $ssh->expect($timeout,' # ');
29 #
30 # send command
31 $ssh->send("uname -a\\n");
32 $ssh->expect($timeout,' # ');
33 print "before: ".$exp->before."\\n";
34 print "after : ".$exp->after."\\n";
35 $ssh->send("exit");
Voir aussi http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod