Libssh::Session(3)

๐Ÿ“ฆ version 1.100.0, in opensuse-leap 16.0

๐Ÿฆ‹ 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\n
  • SSH_AUTH_DENIED โ€” authentication failed : use another method\n
  • SSH_AUTH_PARTIAL โ€” You've been partially authenticated, you still have to use another method\n
  • SSH_AUTH_SUCCESS โ€” Authentication success\n
  • SSH_AUTH_INFO โ€” The server asked some questions. Use auth_kbdint_getnprmopts() and such to retrieve and answer them.\n
  • SSH_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 to chomp() 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 โ€” Returns SSH_OK even 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 that SkipKeyProblem default โ€” 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_ERROR in case of failure. SSH_AGAIN in case of timeout. SSH_OK otherwise.
  • exit_code โ€” The exit code of the command executed. undef when 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 like known_hosts and 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 default identity, id_dsa and id_rsa are 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