×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How capture the outputs of Expect.pm?

How capture the outputs of Expect.pm?

How capture the outputs of Expect.pm?

(OP)
I wrote a piece of perl code to generating ssh keys using Expect.pm. The code can create keys as expected. But I don't know how to capture the finger prints from the output.

CODE

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; 

Here is the output:

CODE

./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)# 

I guess the info is in the Glob? But how to parse the Glob?

Thanks for the help.

RE: How capture the outputs of Expect.pm?

(OP)
I got the solution. Hope it helps.

CODE --> perl

my $output;
  my $session=Expect->spawn($cmd) or die "Error calling external program:  $!\n";
  $session->expect(10,
    [ qr/passphrase/i, sub { my $self = shift;
        $self->send("$passwd\n");
        exp_continue; }],
    [ qr/$comments/i, sub { my $self = shift;
        $output = $self->exp_before;
        exp_continue; }],
  );
  print $output; 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close