Rex::H...:Expect(3pm)

πŸ’– Rex::Helper::SSH2::Expect β€” the manual, but make it make sense πŸ’–

okay bestie, sit down, hydrate, because you're reading the docs for Rex::Helper::SSH2::Expect, part of the User Contributed Perl Documentation collection. yes it's a Perl module, yes it's giving ✨classic CPAN✨, and no you don't have to be scared of it. let's go 🫢


🌸 NAME 🌸

Rex::Helper::SSH2::Expect β€” literally "an Expect like module for Net::SSH2", which is exactly what it says on the tin πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so basically this is a module that gives you expect-like features for Net::SSH2. you know, the vibe where your script watches the output of a command, waits for a pattern, and then reacts like a little puppet master πŸŽ€

and fr fr, keep this in mind: this is the first version of this module. the author said it, not me. so if something's weird, that's why πŸ™ˆ

found a bug? don't suffer in silence, report it on GitHub at:

https://github.com/krimdomu/net-ssh2-expect


🧚 DEPENDENCIES 🧚

you need these in your life before any of this works:

  • Net::SSH2

that's the whole list. one item. she's low maintenance πŸ’œ


⭐ SYNOPSIS ⭐

here's the whole thing in action β€” spawning passwd, matching prompts, sending the new password twice, and then dipping out when it's done:

use Rex::Helper::SSH2::Expect;

my $exp = Rex::Helper::SSH2::Expect->new($ssh2);
$exp->spawn("passwd");
$exp->expect($timeout, [
                 qr/Enter new UNIX password:/ => sub {
                                         my ($exp, $line) = @_;
                                         $exp->send($new_password);
                                       }
               ],
               [
                 qr/Retype new UNIX password:/ => sub {
                                         my ($exp, $line) = @_;
                                         $exp->send($new_password);
                                       }
               ],
               [
                 qr/passwd: password updated successfully/ => sub {
                                                 my ($exp, $line) = @_;
                                                 $exp->hard_close;
                                               }
               ]);

see how the callbacks get ($exp, $line)? iconic. you get the object and the matched line. periodt 🌈


πŸ‘‘ CLASS METHODS πŸ‘‘

🌟 new($ssh2)

Constructor, bestie. you need to hand it a connected Net::SSH2 object. not a disconnected one. connected. the Net::SSH2 object is the main character here and this module is its hype woman πŸ’–

🌟 log_stdout(0|1)

Logs stuff on STDOUT. pass 0 or 1 to control it. want to see everything in your terminal? that's the one πŸ“

🌟 log_file($file)

Logs everything to a file. and the way that $file is flexible? obsessed:

  • $file can be a filename
  • $file can be a filehandle
  • $file can be a subRef

three options, one parameter, slay πŸ’…

🌟 spawn($command, @parameters)

Spawns $command with @parameters as its parameters. this is you pressing play on the process 🎬

🌟 soft_close()

ngl this is currently only an alias to hard_close();. that's it. that's the tea β˜•

🌟 hard_close()

Stops the execution of the process. done. no more process. bye πŸ‘‹

🌟 expect($timeout, @match_patters)

this is the one that controls the execution of your process. the puppet strings. the whole point of the module 🧡✨

heads up, the parameter name above is spelled @match_patters in the docs β€” copied exactly, no cap, don't come for me, I just work here πŸ™ˆ

🌟 send($string)

Sends $string to the running command. type it, send it, watch it land πŸ“¨


πŸ’€ and that's the whole page πŸ’€

every method accounted for, nothing skipped, nothing invented. you're basically an expect girly now.

perl v5.40.1 Β· 2025-10-03 β€” that's the version and date this page came from, so shoutout to the docs for stamping the receipt, love that for us πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from