Help with Piped command, capturing output 
Author Message
 Help with Piped command, capturing output

Here's my little program called myPGP.pl running on W2k:

   use strict;

   my $passphrase = shift;
   my $pubKey = shift;
   my $message = shift;

   open(OUTPUT,"|pgp -feast -m -z $passphrase $pubKey");
   print OUTPUT $message;
   close OUTPUT;

   print "\n\nDONE\n";

I call this program from the command line, and pass to it my PGP
password, public key, and the message I want to encrypt (as a glob of
text).

For example:


text"

This does about 98% of what I'm trying to do, which is to pass text to
PGP and encrypt it.  When this is run all output is printed to the
screen (STDOUT), including the encrypted message.  This is fabulous,
however I'd also (or alternatively) like to capture the output to a
variable within this program and then do something else to it (TBD).

I've tried using open2() to do the bidirectional pipe thing, but that
left me thoroughly confused, and didn't even work.  :P  I've also
tried other things like using the pipe() function, but never made any
sense out of that either.

I've read, and experimented quite a bit, and feel as though I've
conspicuously missed something fairly obvious (for which I'll hang my
head in shame if that turns out to be the case).

Can anyone suggest a solution to this problem (with some anecdotal
example, please)?  I know there has to be a way to do this.

Thanks,

Jonathan



Tue, 03 Feb 2004 20:55:58 GMT  
 Help with Piped command, capturing output

Quote:

> Here's my little program called myPGP.pl running on W2k:

>    use strict;

>    my $passphrase = shift;
>    my $pubKey = shift;
>    my $message = shift;

>    open(OUTPUT,"|pgp -feast -m -z $passphrase $pubKey");
>    print OUTPUT $message;
>    close OUTPUT;

>    print "\n\nDONE\n";

> I call this program from the command line, and pass to it my PGP
> password, public key, and the message I want to encrypt (as a glob of
> text).

> For example:


> text"

> This does about 98% of what I'm trying to do, which is to pass text to
> PGP and encrypt it.  When this is run all output is printed to the
> screen (STDOUT), including the encrypted message.  This is fabulous,
> however I'd also (or alternatively) like to capture the output to a
> variable within this program and then do something else to it (TBD).

> I've tried using open2() to do the bidirectional pipe thing, but that
> left me thoroughly confused, and didn't even work.  :P  I've also
> tried other things like using the pipe() function, but never made any
> sense out of that either.

open2 is the way. here's a snippet to get you started:

# We need a library function
   use IPC::Open2;

# Set up the pgp command
  $pgpcmd = "/full/path/to/pgp -feast -m -z $passphrase $pubKey";

# Open the PGP program for bidirectional I/O
   $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");

# Send data to be encrypted to PGP
   print WRITEPGP $text_to_encrypt;

# Encrypt the data
   close(WRITEPGP);

# Get the encrypted data from PGP
   $encrypted_text = do { local $/; <READPGP> };
   close(READPGP);

hth-
--



Wed, 04 Feb 2004 00:18:59 GMT  
 Help with Piped command, capturing output

Quote:

> open2 is the way. here's a snippet to get you started:

> # We need a library function
>    use IPC::Open2;

> # Set up the pgp command
>   $pgpcmd = "/full/path/to/pgp -feast -m -z $passphrase $pubKey";

> # Open the PGP program for bidirectional I/O
>    $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");

> # Send data to be encrypted to PGP
>    print WRITEPGP $text_to_encrypt;

> # Encrypt the data
>    close(WRITEPGP);

> # Get the encrypted data from PGP
>    $encrypted_text = do { local $/; <READPGP> };
>    close(READPGP);

> hth-

Excellent suggestion...the explaination of Open2 was very clear,
however the script hangs (deadlocks?) immediately after the PGP
program starts...right when it normally would return "Passphrase is
good" it just sits there.  :(

Here is your example applied to my code:

  use strict;
  use IPC::Open2;

  my $passphrase = shift;
  my $pubKey = shift;
  my $message = shift;

# Set up the pgp command
  my $pgpcmd = "pgp -feast -m -z $passphrase $pubKey";

# Open the PGP program for bidirectional I/O
  my $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");

# Send data to be encrypted to PGP
  print WRITEPGP $message;

# Encrypt the data
  close(WRITEPGP);

# Get the encrypted data from PGP
  my $encrypted_text = do { local $/; <READPGP> };
  close(READPGP);
  print $encrypted_text;

Any idears on what might be causing the hang (and better still, how to
get around it)?

Thanks again,

Jonathan



Fri, 06 Feb 2004 17:28:54 GMT  
 Help with Piped command, capturing output

Quote:



> > open2 is the way. here's a snippet to get you started:

> > # We need a library function
> >    use IPC::Open2;

> > # Set up the pgp command
> >   $pgpcmd = "/full/path/to/pgp -feast -m -z $passphrase $pubKey";

> > # Open the PGP program for bidirectional I/O
> >    $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");

> > # Send data to be encrypted to PGP
> >    print WRITEPGP $text_to_encrypt;

> > # Encrypt the data
> >    close(WRITEPGP);

> > # Get the encrypted data from PGP
> >    $encrypted_text = do { local $/; <READPGP> };
> >    close(READPGP);

> > hth-

> Excellent suggestion...the explaination of Open2 was very clear,
> however the script hangs (deadlocks?) immediately after the PGP
> program starts...right when it normally would return "Passphrase is
> good" it just sits there.  :(

> Here is your example applied to my code:

>   use strict;
>   use IPC::Open2;

>   my $passphrase = shift;
>   my $pubKey = shift;
>   my $message = shift;

> # Set up the pgp command
>   my $pgpcmd = "pgp -feast -m -z $passphrase $pubKey";

> # Open the PGP program for bidirectional I/O
>   my $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");

> # Send data to be encrypted to PGP
>   print WRITEPGP $message;

> # Encrypt the data
>   close(WRITEPGP);

> # Get the encrypted data from PGP
>   my $encrypted_text = do { local $/; <READPGP> };
>   close(READPGP);
>   print $encrypted_text;

> Any idears on what might be causing the hang (and better still, how to
> get around it)?

> Thanks again,

> Jonathan

hmmm... you might try appending " 2>&1" to $pgpcmd and see what you
get... also: it may not be able to find your config file: as i recall, i
had to put it (pgp.cfg) in the same dir as the script with this is in it:

PubRing="/full/path/to/pubring.pkr"
Verbose=2

this was with pgp 5.x for irix 6.x, so YMMV...

hth-
--



Sat, 07 Feb 2004 05:50:32 GMT  
 Help with Piped command, capturing output

Quote:

> hmmm... you might try appending " 2>&1" to $pgpcmd and see what you
> get... also: it may not be able to find your config file: as i recall, i
> had to put it (pgp.cfg) in the same dir as the script with this is in it:

> PubRing="/full/path/to/pubring.pkr"
> Verbose=2

> this was with pgp 5.x for irix 6.x, so YMMV...

> hth-

No luck here either.  Lame.

JC



Sat, 14 Feb 2004 20:59:45 GMT  
 Help with Piped command, capturing output
[snip]

Quote:
> This does about 98% of what I'm trying to do, which is to pass text to
> PGP and encrypt it.  When this is run all output is printed to the
> screen (STDOUT), including the encrypted message.  This is fabulous,
> however I'd also (or alternatively) like to capture the output to a
> variable within this program and then do something else to it (TBD).

It's oft been said that the first 90% of the job takes 10% of the time,
and the last 10% of the job takes 90% of the time.

Considering how your problem most likely derives from dealing with
processes, either the fault of pgp, or win2k, or open2, you might
instead want a pure-perl solution which does not run any seperate
process.  Specifically, Crypt::OpenPGP.

use strict;


my $pgp = Crypt::OpenPGP->new;
my $ciphertext = $pgp->encrypt(
        Passphrase => $passphrase,
        Recipients => $pubKey,
        Data => $message,
        Armour => 1, # ascii-armour the output.
);

die $pgp->errstr if !$ciphertext;
print $ciphertext;

--
I'm not a programmer but I play one on TV...



Sun, 15 Feb 2004 03:42:45 GMT  
 Help with Piped command, capturing output

Quote:



> > hmmm... you might try appending " 2>&1" to $pgpcmd and see what you
> > get... also: it may not be able to find your config file: as i recall,
> > i
> > had to put it (pgp.cfg) in the same dir as the script with this is in
> > it:

> > PubRing="/full/path/to/pubring.pkr"
> > Verbose=2

> > this was with pgp 5.x for irix 6.x, so YMMV...

> > hth-

> No luck here either.

what worked and what didn't?

Quote:
> Lame.

excuse me? "Lame"?
--



Sun, 15 Feb 2004 06:38:39 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. capturing output of piped commands

2. Help: capturing pipe output

3. Help: capturing pipe output

4. help with capturing output of open command

5. capturing command line error or output from `<command>`

6. Newbie trying to capture(control) output from pipe

7. Non-intermeshing output streams when capturing STDOUT in a pipe

8. capturing output of FILEHANDLE "| command"

9. capturing output from a remote command (how?)

10. Capturing System Command Output with Perl on Windows/Apache

11. capturing output of executed system command

12. Capture command output into list?

 

 
Powered by phpBB® Forum Software