Automate and capture login request through perl expect

I am trying to automate a Perl login prompt

I use SSHGetCredentials('login_prompt' => 1)to create a login prompt, which is a perl Expect::SSHlibrary function

Here is what my code looks like, REST::Client->getCredentials()internally calls SSHGetCredentials('login_prompt' => 1), which calls a login call and returns the entered user ID and password.

use Expect;
my $command = "perl -e 'use REST::Client; REST::Client->getCredentials()'";
my $exp = Expect->spawn($command) || die "i am done";
$exp->expect(undef, 'Login on Remote:');
$exp->send("user\n");
$exp->expect(undef, 'Password of user:');
$exp->send("12345\n");

Now I want to check if the username or password is what I passed, so I want to capture and compare them.

How can I capture them?

I tried this:

use Expect;
my $command = "perl -e 'use REST::Client; \$tmp = REST::Client->getCredentials();print \$tmp->{'username'};print \" \";print \$tmp->{'password'};'";
my $exp = Expect->spawn($command) || die "i am done";
$exp->expect(undef, 'Login on Remote:');
$exp->send("user\n");
$exp->expect(undef, 'Password of user:');
$exp->send("12345\n");
my $value = $exp->expect(1);

but $valueit has nothing, it $exp->expect(1)just prints the values ​​in STDOUT.

+4
source share
1 answer

, ,

use Expect;
my $command = "perl -e 'use REST::Client; \$tmp = REST::Client->getCredentials();print \$tmp->{'username'};print \" \";print \$tmp->{'password'};'";
my $exp = Expect->spawn($command) || die "i am done";
$exp->expect(undef, 'Login on Remote:');
$exp->send("user\n");
$exp->expect(undef, 'Password of user:');
$exp->send("12345\n");
if ($exp->expect(undef,'-re','user 12345') == 1) {
    print "matched\n";
}
+1

Source: https://habr.com/ru/post/1648273/


All Articles