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

Hi all,

Please find the below code in which i am able to get the user name and password and pass it to ssh to login, but after nothing happens, it just stays there without any further action. Please let me know, what is that i am missing.

use strict; use warnings; use Net::SSH::Perl; use Net::SSH2; $ENV{HOME} = "PATH"; print "Server Name to Connect:"; chomp($host = <STDIN>); print "User Name to Login:";chomp($LoginName = <STDIN>); print "Password for $LoginName:";chomp($pswd = <STDIN>); my $command = "date"; my $ssh = Net::SSH::Perl->new($host); my $status = $ssh->login($LoginName,$pswd); my $stdout; my $stderr; my $exit; if ($@) { if ($status =~ m/Permission denied/i) { print "Authentication failed\n"; print "Password for $LoginName:";chomp($pswd = <STDIN> +); exit 1; } } eval { ($stdout,$stderr,$exit) = $ssh->cmd($command); }; print "$stdout\n";

But if i enter a wrong password, i get permission denied and not Authentication failed message, am i doing something wrong at the eval?

Replies are listed 'Best First'.
Re: Net::SSH::Perl login
by Corion (Patriarch) on Jun 11, 2014 at 10:27 UTC

    Why do you have the eval { ... } there?

    Maybe you want to inspect what happened in the eval { ... } block?

    Consider looking at eval and its usage of $@. Maybe you want to print it, just in case there was an unexpected error?

      Even after removing eval function, still the cursor just blinks and doesn't go further to fetch the command result. I changed my code to be very simple like below:

      use Strict; use warnings; use Net::SSH::Perl; use Net::SSH2; $ENV{HOME} = "PATH"; print "Server Name to Connect:"; chomp($host = <STDIN>); print "User Name to Login:";chomp($LoginName = <STDIN>); print "Password for $LoginName:";chomp($pswd = <STDIN>); #!/usr/bin/perl -w my $command = "date"; my $ssh = Net::SSH::Perl->new($host); my $status = $ssh->login($LoginName,$pswd); my($stdout,$stderr,$exit) = $ssh->cmd($command); print "$stdout\n";

        Hi,

        In your first post eval block, you can print the $@ variable value, it will print any error message if any.

        Here, did you print the $stderr,$exit variables ?

        Enable debugging, This will give you trace information from the cmd method,and should give you some clues about what is going wrong.

        my $ssh = Net::SSH::Perl->new($remote_host, debug => 1);


        All is well