Net-SSH2

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

        - Several documentation improvements.

0.59_08  2016-04-18
        - Get extended diagnosis messages when $ENV{AUTOMATED_TESTING}
          is set.

0.59_07  2016-04-17
        - In auth, do not call authentications methods unless they are
          supported by the server.
        - Fix minor scp_get issue
        - Add workaround for getpwuid not being available on MS
          Windows.
        - Several documentation fixes.

0.59_06  2016-04-17
        - Add method die_with_error into Net::SSH2::SFTP
        - Several documentation improvements.

0.59_05  2016-04-16
        - Switch the order of "policy" and "known_host_path" arguments
          in method "check_hostkey".

example/read.pl  view on Meta::CPAN


use Net::SSH2;
use IO::Scalar;

my $ssh2 = Net::SSH2->new;
$ssh2->connect('localhost') or $ssh2->die_with_error;
$ssh2->check_hostkey('ask') or $ssh2->die_with_error;

# use an interactive authentication method with default callback
# (if a password is provided here, it will forward it without prompting)
$ssh2->auth(username => scalar getpwuid($<), interact => 1)
    or $ssh2->die_with_error;

sub _read {
    my $handle = shift;
    while (my $line = <$handle>) {
        chomp $line;
        $line =~ s/:.*$//;
        print "found user '$line'\n";
    }
}

example/scat.pl  view on Meta::CPAN

use Getopt::Long;

my ($host, $port, $user, $pwd);
GetOptions("host|h=s" => \$host,
           "port|p=s" => \$port,
           "user|u=s" => \$user,
           "password|pwd|pw|w=s" => \$pwd)
    or die "Failed to process arguments";

$host // die "hostname missing";
$user //= getpwuid $<;
$port //= '22';

my $ssh2 = Net::SSH2->new();
# $ssh2->debug(1);
$ssh2->connect($host, $port) or $ssh2->die_with_error;
$ssh2->check_hostkey  or $ssh2->die_with_error;

if ($pwd) {
    $ssh2->auth_password($user, $pwd);
}

lib/Net/SSH2.pm  view on Meta::CPAN

sub _local_user {
    for (qw(USER LOGNAME)) {
        return $ENV{$_} if defined $ENV{$_}
    }

    local ($@, $SIG{__DIE__}, $SIG{__WARN__});

    my $u = eval { getlogin };
    return $u if defined $u;

    eval { getpwuid $< }
}

my $password_when_you_mean_passphrase_warned;
sub auth {
    my ($self, %p) = @_;

    $self->_set_error(LIBSSH2_ERROR_AUTHENTICATION_FAILED(),
                      "Authentication failed"); # default error

    $p{username} = _local_user unless defined $p{username};

lib/Net/SSH2.pm  view on Meta::CPAN

        last if $rc or $self->error != LIBSSH2_ERROR_AUTHENTICATION_FAILED();
        my $ofh = select STDERR; local $|= 1; select $ofh;
        $self->_print_stderr("Password authentication failed!\n");
    }
    return $rc;
}

sub _local_home {
    return $ENV{HOME} if defined $ENV{HOME};
    local ($@, $SIG{__DIE__}, $SIG{__WARN__});
    my $home = eval { (getpwuid($<))[7] };
    return $home;
}

my $check_hostkey_void_ctx_warned;
sub check_hostkey {
    my ($self, $policy, $path, $comment) = @_;

    defined wantarray or $check_hostkey_void_ctx_warned++ or
        warnings::warnif($self, "Calling check_hostkey in void context is useless");

t/Net-SSH2.t  view on Meta::CPAN

is(length $sha1, 20, 'have SHA1 hostkey hash');

ok($ssh2->check_hostkey('advisory', $known_hosts), "check remote key - advisory")
    or diag(join " ", "Error:", $ssh2->error);

ok($ssh2->check_hostkey($policy, $known_hosts), "check remote key - ask")
    or diag(join " ", "Error:", $ssh2->error);

# (3) authentication methods
unless ($user) {
    my $def_user = eval { getpwuid $< };
    $user = $ssh2->_ask_user("Enter username" . ($def_user ? " [$def_user]: " : ": "), 1);
    $user = $def_user unless defined $user and length $user;
}
my $auth = $ssh2->auth_list($user);
ok($auth, "authenticate: $auth");
my @auth_methods = split /,/, $auth;
is_deeply(\@auth_methods, [$ssh2->auth_list($user)], 'list matches comma-separated');
ok(!$ssh2->auth_ok, 'not authenticated yet');

# (2) authenticate



( run in 0.385 second using v1.01-cache-2.11-cpan-8d75d55dd25 )