Last active
December 11, 2015 00:49
-
-
Save krimdomu/4519319 to your computer and use it in GitHub Desktop.
Test Script to verify Net::SSH2 installation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Save this file under /tmp/test-ssh.pl, edit the variables and run it with | |
# perl /tmp/test-ssh.pl | |
# | |
use strict; | |
use warnings; | |
# ------- PLEASE SET THE VARS ----------- | |
my $HOST = ''; | |
my $USER = ''; | |
my $PASSWORD = ''; | |
my $PRIVATE_KEY = ''; | |
my $PUBLIC_KEY = ''; | |
# -------------------------------------- | |
# | |
# ============================================================================== | |
# don't do anything below | |
# ============================================================================== | |
# | |
use Net::SSH2; | |
sub run_get_value; | |
sub run_get_first_line; | |
sub print_value; | |
sub print_multi_value; | |
print_value "Operating System", $^O; | |
print_value "Perl Interpreter", $^X; | |
print_value "Perl Path", run_get_first_line "which $^X"; | |
print_value "Net::SSH2 Version", $Net::SSH2::VERSION; | |
print_multi_value "Perl Include Path", \@INC; | |
my $ssh = Net::SSH2->new; | |
test_case("Connection", $ssh->connect($HOST)); | |
my ($auth); | |
if($PRIVATE_KEY && $PUBLIC_KEY) { | |
$auth = $ssh->auth_publickey($USER, $PUBLIC_KEY, $PRIVATE_KEY, $PASSWORD); | |
} | |
else { | |
$auth = $ssh->auth_password($USER, $PASSWORD); | |
} | |
test_case("Autentication", $auth); | |
sub test_case { | |
my ($text, $status) = @_; | |
my $status_text = ($status?"ok":"failed"); | |
print_value($text, $status_text); | |
} | |
sub print_value { | |
my ($text, $value) = @_; | |
printf "%-45s %s\n", $text, $value; | |
} | |
sub print_multi_value { | |
my ($text, $value) = @_; | |
if(ref $value eq "ARRAY") { | |
print "$text\n"; | |
for (@{ $value }) { | |
print "\t$_\n"; | |
} | |
} | |
} | |
sub run_get_value { | |
my ($cmd, $regexp) = @_; | |
my @out = qx{$cmd}; | |
chomp @out; | |
if(! $regexp) { | |
return @out; | |
} | |
} | |
sub run_get_first_line { | |
my @out = run_get_value(@_); | |
return $out[0]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment