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:
$filecan be a filename$filecan be a filehandle$filecan 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
- ubuntu resolute: rex 1.16.1-1 Β· Apache-2.0
- ubuntu stonking: rex 1.16.1-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms