Perl: exemple SSH avec Expect : Différence entre versions

De TechWik
Aller à : navigation, rechercher
(Page créée avec « Example illustrant l'utilisation d'Expect + SSH: #!/bin/env perl use Expect; my $fileTraceExpect = "/tmp/dummy.trace"; my $userlogin="root"; my $userpw="mypa... »)
(Aucune différence)

Version du 3 février 2018 à 18:32

Example illustrant l'utilisation d'Expect + SSH:

 #!/bin/env perl 
 use Expect;
 my $fileTraceExpect = "/tmp/dummy.trace";  
 my $userlogin="root";
 my $userpw="mypasswd";
 my $remotehost="192.168.0.2";
 #
 # Start a sftp connection
 my $exp = new Expect;
 #
 # save results of expect and send commands
 $exp->log_file($fileTraceExpect, "w");  
 # no print on stdout
 $exp->log_stdout(0);                    
 #
 my $timeout = 2;  
 #
 # open ssh connection
 my $command = "ssh ".$userlogin."@".$remotehost."\\n";
 my $ssh= $exp->spawn($command) || die "Cannot spawn $command: $!\\n";
 #
 if ($ssh->expect($timeout, "Are you sure you want to continue connecting")) {
   $ssh->send("yes\\r");
   $ssh->expect($timeout, "Password:");
 }
 #
 $ssh->send("$userpw\\n");
 $ssh->expect($timeout,' # ');
 #
 # send command
 $ssh->send("uname -a\\n");
 $ssh->expect($timeout,' # ');
 print "before: ".$exp->before."\\n";
 print "after : ".$exp->after."\\n";
 $ssh->send("exit");

Voir aussi http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod