El siguiente ejemplo que acompaña a la distribución muestra su uso:
nereida:/tmp/Net-SSH-Perl-1.30/eg> cat -n cmd.pl 1 #!/usr/bin/perl -w 2 # $Id: cmd.pl,v 1.4 2001/02/22 00:14:48 btrott Exp $ 3 4 use strict; 5 6 use Net::SSH::Perl; 7 use Net::SSH::Perl::Cipher; 8 9 chomp(my $this_host = `hostname`); 10 print "Enter a host name to connect to: [$this_host] "; 11 chomp(my $host = <STDIN>); 12 print "\n"; 13 14 print "Enter the port number of the remote sshd: [ssh] "; 15 chomp(my $port = <STDIN>); 16 print "\n"; 17 18 print "Choose a cipher from the list:\n"; 19 my $supp = Net::SSH::Perl::Cipher::supported(); 20 for my $ciph (sort @$supp) { 21 printf " [%d] %s\n", $ciph, Net::SSH::Perl::Cipher::name($ciph); 22 } 23 printf "Enter a number: [%d] ", Net::SSH::Perl::Cipher::id('IDEA'); 24 chomp(my $c = <STDIN>); 25 print "\n"; 26 my $ssh = Net::SSH::Perl->new($host || $this_host, 27 port => $port || 'ssh', 28 cipher => Net::SSH::Perl::Cipher::name($c), 29 debug => 1); 30 31 my $this_user = scalar getpwuid($<); # $< es el uid real de este proceso 32 print "Enter your username on that host: [$this_user] "; 33 chomp(my $user = <STDIN>); 34 35 use Term::ReadKey; 36 37 print "And your password: "; 38 ReadMode('noecho'); 39 chomp(my $pass = ReadLine(0)); 40 ReadMode('restore'); 41 print "\n"; 42 43 $ssh->login($user || $this_user, $pass); 44 45 print "Enter a command to execute: [ls -l] "; 46 chomp(my $cmd = <STDIN>); 47 48 my($out, $err) = $ssh->cmd($cmd || "ls -l"); 49 print $out;
Este es el resultado de una ejecución:
localhost:/tmp/Net-SSH-Perl-1.30/eg> perl cmd.pl Enter a host name to connect to: [localhost] rmachine Enter the port number of the remote sshd: [ssh] Choose a cipher from the list: [1] IDEA [2] DES [3] DES3 [5] RC4 [6] Blowfish Enter a number: [1] localhost: Reading configuration data /home/pl/.ssh/config localhost: Reading configuration data /etc/ssh_config localhost: Connecting to rmachine, port 22. localhost: Remote version string: SSH-2.0-OpenSSH_4.3p2 Debian-5 localhost: Remote protocol version 2.0, remote software version OpenSSH_4.3p2 Debian-5 localhost: Net::SSH::Perl Version 1.30, protocol version 2.0. localhost: No compat match: OpenSSH_4.3p2 Debian-5. localhost: Connection established. Enter your username on that host: [pl] loginname And your password: localhost: Sent key-exchange init (KEXINIT), wait response. localhost: Algorithms, c->s: 3des-cbc hmac-sha1 none localhost: Algorithms, s->c: 3des-cbc hmac-sha1 none localhost: Entering Diffie-Hellman Group 1 key exchange. localhost: Sent DH public key, waiting for reply. localhost: Received host key, type 'ssh-dss'. localhost: Permanently added 'rmachine' to the list of known hosts. localhost: Computing shared secret key. localhost: Verifying server signature. localhost: Waiting for NEWKEYS message. localhost: Enabling incoming encryption/MAC/compression. localhost: Send NEWKEYS, enable outgoing encryption/MAC/compression. localhost: Sending request for user-authentication service. localhost: Service accepted: ssh-userauth. localhost: Trying empty user-authentication request. localhost: Authentication methods that can continue: publickey,password. localhost: Next method to try is publickey. localhost: Next method to try is password. localhost: Trying password authentication. localhost: Login completed, opening dummy shell channel. localhost: channel 0: new [client-session] localhost: Requesting channel_open for channel 0. localhost: channel 0: open confirm rwindow 0 rmax 32768 localhost: Got channel open confirmation, requesting shell. localhost: Requesting service shell on channel 0. Enter a command to execute: [ls -l] uname -a localhost: channel 1: new [client-session] localhost: Requesting channel_open for channel 1. localhost: Entering interactive session. localhost: Sending command: uname -a localhost: Requesting service exec on channel 1. localhost: channel 1: open confirm rwindow 0 rmax 32768 localhost: channel 1: rcvd eof localhost: channel 1: output open -> drain localhost: input_channel_request: rtype exit-status reply 0 localhost: channel 1: rcvd close localhost: channel 1: input open -> closed localhost: channel 1: close_read localhost: channel 1: obuf empty localhost: channel 1: output drain -> closed localhost: channel 1: close_write localhost: channel 1: send close localhost: channel 1: full closed Linux rmachine 2.6.15-1-686-smp #2 SMP Mon Mar 6 15:34:50 UTC 2006 i686 GNU/LinuxDependiendo de la máquina es posible que haya que cambiar el cifrado o que ninguno de los cifrados proveídos funcione.
Casiano Rodríguez León