我编写了一段 perl 代码来使用 Expect.pm 生成 ssh 密钥。该代码可以按预期创建密钥。但我不知道如何从输出中捕获指纹。
use Expect;
my $passwd = "abcdefg";
my $keyFile = './mykey';
my $cmd = qq/ssh-keygen -t rsa -b 2048 -C "my comments" -f $keyFile/;
print "\nCMD: $cmd\n\n";
my @output;
my $session=Expect->spawn($cmd) or die "Error calling external program: $!\n";
unless ($session->expect(5,"Enter passphrase \(empty for no passphrase\): ")) {};
print $session "$passwd\r";
unless (@output = $session->expect(5,"Enter same passphrase again: ")) {}; # Capture the output
print $session "$passwd\r";
$session->soft_close();
my $i = 1;
foreach my $e (@output) {
if($e) {
print "\$i = $i, Type of \$e: #". ref($e) . "#, Value of \$e: #$e#\n";
}
else {
print "$i, NULL Element!\n";
}
$i++;
}
exit;
下面是输出:
% ./test.pl
CMD: ssh-keygen -t rsa -b 2048 -C "my comments" -f ./mykey
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ./mykey.
Your public key has been saved in ./mykey.pub.
**The key fingerprint is:
df:aa:35:19:28:06:0e:97:ec:6d:83:26:b9:01:4f:50** my comments
The key's randomart image is:
+--[ RSA 2048]----+
|..E |
| . . . |
|. o = |
| + * + . |
| = = * S . |
| = o o . + |
| . = . |
| . o |
| ... |
+-----------------+
$i = 1, Type of $e: ##, Value of $e: #1#
2, NULL Element!
$i = 3, Type of $e: ##, Value of $e: #Enter same passphrase again: #
$i = 4, Type of $e: ##, Value of $e: #
#
5, NULL Element!
$i = 6, Type of $e: #Expect#, Value of $e: #Expect=GLOB(0x1e74b38)#
我猜信息在 Glob 中?但是如何解析 Glob?谢谢您的帮助。