Perl: telnet dans un script perl : Différence entre versions

De TechWik
Aller à : navigation, rechercher
(Page créée avec « #!/bin/env perl use strict; use Getopt::Long; use FindBin; use lib "$FindBin::Bin"; use Telnet ; # Variables for telnet call my $session ; my $remote... »)
 
(Aucune différence)

Version du 3 février 2018 à 18:30

 #!/bin/env perl
 use strict;
 use Getopt::Long;
 use FindBin;
 use lib "$FindBin::Bin";
 use Telnet ;
 # Variables for telnet call
 my $session ;
 my $remoteaddr = "192.168.10.10" ;
 my $username = "user1" ;
 my $password = 'mypw' ;
 my $port = 23 ;
 my $response = "";
 my $resultFile = "/tmp/dummylog.txt";
 my $debug=1;    # set 0 for no debug information
 #-------------------------------------------------------------------------------
 # show debug information
 sub debug {
   print @_ if ($debug);
 }
 #-------------------------------------------------------------------------------
 # retrieve and evaluate the command's response
 sub getResponse {
   my @response = @_;
   my $text = "";
   foreach( @response ) {
     $text = $text . $_;
   }
   return $text;
 }
 #-------------------------------------------------------------------------------
 # retrieve and evaluate the command's response
 sub sendCommandAndGetResponse {
   my ($cmd, $prompt) = @_;
   if ($prompt) {
     $response = getResponse( $session -> cmd (String => "$cmd",
                     Timeout => 10  ));
   } else {
     $response = getResponse( $session -> cmd (String => "$cmd",
                     Timeout => 10, Prompt => "$prompt" ));
   }
   if( $response > "1" ) {
     print "error: $response.\\n";
     exit;
   } else {
     debug("$response.\\n");
   }
 }
 #-------------------------------------------------------------------------------
 # check options
 #
   usage() unless ( GetOptions(
                     "addr=s"  , \\$remoteaddr,
                     "user=s"  , \\$username,
                     "passw=s" , \\$password,
     "outfile=s" , \\$resultFile,
                   )
       );
 # Start a Telnet session to remote address
 if ( $remoteaddr ne "" ) {
   eval { $session = Telnet -> new (Timeout => 10,
                             Prompt => "/.*$username #/",
                             # Prompt => "/System>/",
               Port => $port) or exit ; };
   if( $@ ) {
     my $text = $@;
     $text =~ s/.+://;
     $text =~ s/at.*//;
     print "Telnet session could not be instanciated: $text.\\n";
     exit;
   }
   #
   eval { $session -> open ($remoteaddr) ; };
   if( $@ ) {
     my $text = $@;
     $text =~ s/.+://;
     $text =~ s/at.*//;
     print "$remoteaddr: Telnet could not be opened: $text.\\n";
     exit;
   }
   #
   debug("$remoteaddr: Telnet opened.\\n");
   eval { $session -> login (Name => $username,
                      Password => $password) ; };
   if( $@ ) {
     debug("$remoteaddr: unsuccessful login attempt!\\n");
     exit;
   }
   else {
     debug("$remoteaddr: successfully logged in.\\n");
   }
   #
   # prevent from receiving garbage
   sendCommandAndGetResponse("uname -a");
   debug ("server info: $response\\n");
 }