我正在尝试在Perl中自动化登录提示
我正在使用SSHGetCredentials(‘login_prompt’=> 1)来生成登录提示,这是perl的Expect :: SSH库的一个功能
这就是我的代码的样子,这里REST :: Client-> getCredentials()在内部调用SSHGetCredentials(‘login_prompt’=> 1),它将调用登录提示并返回输入的用户ID和密码.
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");
现在我想验证用户名或密码是否是我通过的,所以我想捕获它们并进行比较.
我该如何捕捉它们?
我试过这个:
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);
但是$value没有任何东西和$exp-> expect(1)只打印STDOUT中的值.
最佳答案 不确定它是否是最好的方式,但它的工作原理
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";
}