Perl SSH

El siguiente código proporciona un comando pssh que es una versión Perl del comando ssh. Al estar todo el código escrito en Perl nos proporciona un cliente bastante independiente del sistema operativo:

pp2@nereida:/tmp/Net-SSH-Perl-1.30/eg$ cat -n pssh
 1  #!/usr/bin/perl -w
 2  # $Id: pssh,v 1.16 2001/04/28 05:28:40 btrott Exp $
 3
 4  use strict;
 5
 6  use Net::SSH::Perl;
 7  use Getopt::Long;
 8
 9  my %opts;
10  Getopt::Long::Configure('no_ignore_case');
11  GetOptions(\%opts, "c=s", "l=s", "p=i", "i=s@", "v", "t", "C", "o=s@", "V", "2", "1");
12
13  if ($opts{V}) {
14      print Net::SSH::Perl->version_string, "\n";
15      exit;
16  }
17
18  my($host, $cmd) = @ARGV;
19
20  die "usage: pssh [options] hostname [command]"
21      unless $host;
22
23  my %cipher_map = (
24      idea => 'IDEA',
25      des  => 'DES',
26      '3des' => 'DES3',
27      blowfish => 'Blowfish',
28  );
29
30  my %args;
31  $args{compression} = 1 if $opts{C};
32  $args{cipher} = $cipher_map{ $opts{c} } || $opts{c} if $opts{c};
33  $args{port} = $opts{p} if $opts{p};
34  $args{debug} = 1 if $opts{v};
35  $args{identity_files} = $opts{i} if $opts{i};
36  $args{use_pty} = 1 if $opts{t};
37  $args{options} = $opts{o} if $opts{o};
38  $args{protocol} = 2 if $opts{2};
39  $args{protocol} = 1 if $opts{1};
40
41  my $ssh = Net::SSH::Perl->new($host, %args);
42  $ssh->config->set('interactive', 1)
43      unless defined $ssh->config->get('interactive');
44  $ssh->login($opts{l});
45
46  if ($cmd) {
47      my($out, $err, $exit) = $ssh->cmd($cmd);
48      print $out if $out;
49      print $err if $err;
50  }
51  else {
52      eval "use Term::ReadKey;";
53      ReadMode('raw');
54      eval "END { ReadMode('restore') };";
55      $ssh->shell;
56      print "Connection to $host closed.\n";
57  }

Véase un ejemplo de ejecución:

pp2@local:/tmp$ pssh -l casiano remote.pcg.ull.es
Last login: Wed Jul  2 10:06:57 2008 from local
casiano@remote:~$

Casiano Rodríguez León
2010-03-22