我正在尝试在 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";
}
关于perl - 通过 perl 的 expect 自动化和捕获登录提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38440940/