Libssh::Session(3)
๐ฆ NAME ๐ฆ
Okay so bestie, welcome to Libssh::Session(3) โ section 3 of the User Contributed Perl Documentation, no cap โจ โ the page that gives you support for the SSH protocol via libssh. That's the whole vibe. SSH, but from Perl, through libssh. Slay.
๐ธ SYNOPSIS ๐ธ
Here's the iconique little script they hand you, verbatim, copy it exactly:
!/usr/bin/perl
use strict;
use warnings;
use Libssh::Session qw(:all);
my $session = Libssh::Session->new();
if (!$session->options(host => "127.0.0.1", port => 22)) {
print $session->error() . "\n";
exit(1);
}
if ($session->connect() != SSH_OK) {
print $session->error() . "\n";
exit(1);
}
if ($session->auth_password(password => "password") != SSH_AUTH_SUCCESS) {
printf("auth issue: %s\n", $session->error(GetErrorSession => 1));
exit(1);
}
print "== authentification succeeded\n";
sub my_callback {
my (%options) = @_;
print "================================================\n";
print "=== exit = " . $options{exit} . "\n";
if ($options{exit} == SSH_OK || $options{exit} == SSH_AGAIN) { # AGAIN means timeout
print "=== exit_code = " . $options{exit_code} . "\n";
print "=== userdata = " . $options{userdata} . "\n";
print "=== stdout = " . $options{stdout} . "\n";
print "=== stderr = " . $options{stderr} . "\n";
} else {
printf("error: %s\n", $session->error(GetErrorSession => 1));
}
print "================================================\n";
#$options{session}->add_command(command => { cmd => 'ls -l', callback => \&my_callback, userdata => 'cmd 3'});
}
$session->execute(
commands => [
{ cmd => 'ls -l', callback => \&my_callback, userdata => 'cmd 1'},
{ cmd => 'ls wanterrormsg', callback => \&my_callback, userdata => 'cmd 2 error'}
],
timeout => 60, timeout_nodata => 30, parallel => 4
);
exit(0);
See that # AGAIN means timeout comment in there? Keep it, it's doing important work ๐
๐ DESCRIPTION ๐
"Libssh::Session" is a perl interface to the libssh library (<http://www.libssh.org>) ๐ซถ. Real talk though: it doesn't support all of the library โ it's working in progress, bestie, we're all just doing our best out here ๐ญ.
Right now, what it actually does is let you authenticate and execute commands on a SSH server. That's the whole flex. And honestly? It ate. ๐ฅ
โจ METHODS โจ
๐ new
Create a new Session object, duh:
my $session = Libssh::Session->new();
Main character energy from the jump ๐
๐ auth_publickey_auto ([ OPTIONS ])
Tries to automatically authenticate with public key and "none". Returns SSH_AUTH_SUCCESS if it succeeds ๐.
"OPTIONS" get passed in a hash-like fashion, key and value pairs, iykyk. The possible option is:
passphraseโ passphrase for the private key (if it's needed. Otherwise don't set the option).
๐ auth_list ([ OPTIONS ])
Tries to retrieve a list of accepted authentication methods ๐. Returns a bitfield of these values, all of them, in a row:
SSH_AUTH_METHOD_UNKNOWN SSH_AUTH_METHOD_NONE SSH_AUTH_METHOD_PASSWORD SSH_AUTH_METHOD_PUBLICKEY SSH_AUTH_METHOD_HOSTBASED SSH_AUTH_METHOD_INTERACTIVE SSH_AUTH_METHOD_GSSAPI_MIC
Heads up bestie: the function auth_none() must be called first before the methods are available ๐ซ
๐ auth_password ([ OPTIONS ])
Try to authenticate by password. Returns SSH_AUTH_SUCCESS if it succeeds โ
.
"OPTIONS" get passed in a hash-like fashion, using key and value pairs. The possible option is:
passwordโ the doc literally describes this one as "passphrase for the private key (if it's needed. Otherwise don't set the option)." Yes, that's word-for-word what it says ๐ copying is a love language.
โจ๏ธ auth_kbdint ([ OPTIONS ])
Try to authenticate through the "keyboard-interactive" method ๐ง. Returns one of the following:
SSH_AUTH_ERRORโ a serious error happened\nSSH_AUTH_DENIEDโ authentication failed : use another method\nSSH_AUTH_PARTIALโ You've been partially authenticated, you still have to use another method\nSSH_AUTH_SUCCESSโ Authentication success\nSSH_AUTH_INFOโ The server asked some questions. Useauth_kbdint_getnprmopts()and such to retrieve and answer them.\nSSH_AUTH_AGAINโ In nonblocking mode, you've got to call this again later.
(The \ns are part of the documentation's own formatting, so I kept them, don't come for me ๐)
๐ท๏ธ auth_kbdint_getname ([ OPTIONS ])
Get the "name" of the message block. Returns undef if there isn't one or it couldn't be retrieved ๐ซฅ.
๐ auth_kbdint_getinstruction ([ OPTIONS ])
Get the "instruction" of the message block. Returns undef if there isn't one or it couldn't be retrieved ๐ซฅ.
๐ข auth_kbdint_getnprmopts ([ OPTIONS ])
Get the number of authentication questions given by the server ๐. This function can be used once you've called auth_kbdint() and the server responded with SSH_AUTH_INFO.
๐ฌ auth_kbdint_getprompt ([ OPTIONS ])
Get a prompt from a message block. You can use this once you've called auth_kbdint() and the server responded with SSH_AUTH_INFO, to retrieve one of the authentication questions ๐ฉ. The total number of quesitons can be retrieved with auth_kbdint_getnprmopts(). Returns a reference to a hash table.
"OPTIONS" come in hash-like fashion, key and value pairs. The possible option:
indexโ The number of the prompt you want to retrieve.
And the hash table it hands back has these attributes bestie:
textโ The prompt text.echoโ'0'or'1'bool value whether or not the user's input should be echoed back.
โ๏ธ auth_kbdint_setanswer ([ OPTIONS ])
Set the answer to a prompt from a message block ๐ฎ.
"OPTIONS" in hash-like fashion, key and value pairs. Possible options:
indexโ The number of the prompt you want to give an answer to.answerโ The answer to the question. If reading ipnut from <STDIN> make sure tochomp()and append a"\0"character, otherwise it doesn't seem to work.
That "\0" thing is not a suggestion, it's a whole lifestyle. Skip it and it just won't work ๐ญ
๐ซฅ auth_none ([ OPTIONS ])
Try to authenticate through the "none" method. Returns SSH_AUTH_SUCCESS if it succeeds โจ.
๐ connect ([ OPTIONS ])
Connect to the ssh server. Returns SSH_OK if no error ๐. By default, the connect does the server check verification.
"OPTIONS" are hash-like, key and value pairs. Possible options:
connect_onlyโ Set the value to'1'if you want to do the server check verification yourself.SkipKeyProblemโ ReturnsSSH_OKeven if there is a problem (server known changed or server found other) with the ssh server (set by default. Set'0'to disable).
Bestie, peep thatSkipKeyProblemdefault โ it's on out of the box. If you want to actually hear about a changed/other server, set it to'0'๐
๐ disconnect ()
Disconnect from a session. The session can then be reused to open a new session โป๏ธ.
The method take care of the current open channels.
โ ๏ธ Warning: in many case, you should let the destructor do it!
โก execute_simple ([ OPTIONS ])
Execute a single command. Returns a reference to a hash table ๐ฏ.
"OPTIONS" hash-style, key and value pairs. Possible options:
cmdโ The command to execute.timeoutโ Set the timeout in seconds for the global command execution (By default:300).timeout_nodataโ Set the timeout in seconds for no data received (By default:120).
The hash table it returns has these attributes:
exitโSSH_ERRORin case of failure.SSH_AGAINin case of timeout.SSH_OKotherwise.exit_codeโ The exit code of the command executed.undefwhen timeout.stdoutโ The stdout of the executed command.stderrโ The stderr of the executed command.
๐ execute ([ OPTIONS ])
Execute multiple commands ๐โโ๏ธ๐จ. If an error occured, please look how to handle it with the callback functions.
"OPTIONS" hash-like, key and value pairs. Possible options:
commandsโ Reference to an array of hashes.timeoutโ Set the timeout in seconds for the global command execution (By default:300). Each command has its own timeout.timeout_nodataโ Set the timeout in seconds for no data received (By default:120). Each command has its own timeout.parallelโ Set the number of parallel commands launched (By default:4).
โ ๏ธ Warning: Execution times of callbacks count in timeout! Maybe you should save the datas and manages after the execute function. Like, do not be doing your whole side quest inside the callback, bestie, the clock is ticking ๐ญ
Look the example above to see how to set the array for commands ๐
๐ฉน error ( )
Returns the last error message. Returns undef if no error ๐ซถ.
(And yeah, the example up top calls it like $session->error(GetErrorSession => 1) โ that's how it shows up in the synopsis, keep it as-is ๐
)
๐งฌ get_publickey_hash ([ OPTIONS ])
Get a hash of the public key. If an error occured, undef is returned ๐ถโ๐ซ๏ธ.
"OPTIONS" hash-like, key and value pairs. Possible option:
Typeโ Hash type to used. Default:SSH_PUBLICKEY_HASH_SHA1. Can be:SSH_PUBLICKEY_HASH_MD5.
๐๏ธ get_server_publickey ( )
Returns the server public key. If an error occured, undef is returned.
โ ๏ธ Warning: should be used if you know what are you doing! Respectfully, do not just vibe-check this one ๐ซฃ
โ๏ธ options ([ OPTIONS ])
Set options for the ssh session. If an error occured, != SSH_OK is returned ๐.
"OPTIONS" hash-style, key and value pairs. Possible options, the full set:
Hostโ The hostname or ip address to connect to.Userโ The username for authentication.Portโ The port to connect to.Timeoutโ Set a timeout for the connection in seconds.LogVerbosityโ Set the session logging verbosity (can be:SSH_LOG_NOLOG,SSH_LOG_RARE,...)SshDirโ Set the ssh directory. The ssh directory is used for files likeknown_hostsand identity (private and public key). It may include"%s"which will be replaced by the user home directory.KnownHostsโ Set the known hosts file name.Identityโ Set the identity file name (By defaultidentity,id_dsaandid_rsaare checked).RaiseErrorโ Die if there is an error (By default:0).PrintErrorโ print in stdout if there is an error (By default:0).
๐ LICENSE ๐
This library is licensed under the Apache License 2.0. Details of this license can be found within the 'LICENSE' text file ๐.
๐ AUTHOR ๐
Quentin Garnier <qgarnier@centreon.com> โ thank you for your service ๐ซถโจ
And that's the page, bestie!! Written by that author up there, shipped as perl v5.42.0, dated 2024-06-23, and it lives at Libssh::Session(3) in the User Contributed Perl Documentation. Go forth and SSH, mwah ๐๐
๐ฆ where this page comes from
- opensuse-leap 16.0: perl-Libssh-Session 1.100.0-bp160.1.12 ยท Apache-2.0
- opensuse-leap 16.0: perl-Libssh-Session 1.100.0-bp160.1.12 ยท Apache-2.0
- opensuse-leap 16.0: perl-Libssh-Session 1.100.0-bp160.1.12 ยท Apache-2.0
- opensuse-leap 16.0: perl-Libssh-Session 1.100.0-bp160.1.12 ยท Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms