Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

SSH into Second Server w/ Net::SSH::Perl

by awohld (Hermit)
on Aug 12, 2010 at 23:43 UTC ( [id://854791]=perlquestion: print w/replies, xml ) Need Help??

awohld has asked for the wisdom of the Perl Monks concerning the following question:

I'm logging into a Solaris machine using Net::SSH::Perl, then I need to SSH from there to a second Solaris machine.

I get in the first computer fine and then when I SSH into the second it hangs. The second computer is asking for a password.

I've tried using the handler register to SSH_SMSG_STDERR_DATA and SSH_SMSG_STDOUT_DATA constants, and various modifications on the Net::SSH::Perl exmaple to send the password when the user is prompted for it but it dosn't work, just hangs. Can someone give me a clue?
use strict; use warnings; use Net::SSH::Perl; use Net::SSH::Perl::Constants qw( :msg ); my $ssh = Net::SSH::Perl->new($user); $ssh->login($username, $password); $ssh->register_handler( SSH_SMSG_STDERR_DATA, sub{ my ( $ssh, $packet ) = @_; my $str = $packet->get_str; if ( $str =~ /Password/ ) { my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str($password); $packet->send; } } ); $ssh->cmd('ssh second_host'); $ssh->cmd('run cmd on second host');
Update: I added the SSH keys so when I do 'ssh second_host' it goes without a problem when I use a SSH client to SSH from the first remote to the second remote. But when I'm using Net::SSH::Perl, it still hangs. The password barrier should be removed.

Replies are listed 'Best First'.
Re: SSH into Second Server w/ Net::SSH::Perl
by atancasis (Sexton) on Aug 13, 2010 at 08:42 UTC

    The default identity filenames used by Net::SSH::Perl for passphrase-less SSH logins does not include the default RSAv2 identity filename (ie: id_rsa). To check if you are using RSA v2, you should have a .ssh/id_rsa file sitting in your home directory, in which case the Net::SSH::Perl object should be instantiated like this:

    my $ssh = Net::SSH::Perl->new($host, identity_files => [ "$ENV{HOME}/.ssh/id_rsa" ]);

    Note the identity_files option. Also, just change the path in case you changed the default identity filename when you generated the SSH keys.

Re: SSH into Second Server w/ Net::SSH::Perl
by Khen1950fx (Canon) on Aug 13, 2010 at 03:20 UTC
    Try using
    use Net::SSH::Perl::Constants qw( SSH_SMSG_STDERR_DATA SSH_CMSG_STDIN_DATA);
    It seemed to work better for me. Here's what I tried:
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; use Net::SSH::Perl::Constants qw( SSH_SMSG_STDERR_DATA SSH_CMSG_STDIN_DATA ); my $host = 'localhost'; my $username = 'user'; my $password = 'password'; my $cmd1 = 'whereis perl && perl -V'; my $ssh = Net::SSH::Perl->new( $host, protocol => 2, 1, debug => 1, ); $ssh->login( $username, $password ); $ssh->register_handler( SSH_SMSG_STDERR_DATA, sub{ my ( $ssh, $packet ) = @_; my $str = $packet->get_str; if ( $str =~ /Password/ ) { $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str($password); $packet->send; } } ); my ($stdout, $stderr, $exit) = $ssh->cmd($cmd1); print $stdout, "\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://854791]
Approved by aquarium
Front-paged by aquarium
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-24 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found