Hi,
I'm trying to write a Perl program that runs a PGP command (using NAI's e-business server) to add a key to my keyring. If the key already exists on the keyring, then the PGP command will fail with an error message. Otherwise it is added, but I need to confirm this with a carriage return (I can't seem to override this with PGP settings despite instructions in the manual...)
So basically I need to write (a carriage return) to STDIN and capture STDOUT or STDERR of the command.
I'm using Open3 and so far I can read from STOUT and STDERR but can't write to STDIN, so everytime I run my program I have to press return myself - but I eventually want to run this as a CGI script...
Here's my code:
#/usr/bin/perl
# program to test open3()
# module imports
use strict;
use warnings;
use diagnostics;
use IPC::Open3;
use IO::Handle;
use FileHandle ();
# variable declarations
my $pid;
my $pid2;
my @errors;
my @output;
my $pgp_command = "pgp --key-add temp_pgp_key.asc --with-private";
open (OUTPUT, ">&STDOUT") # output sent directly
or die "Can't dup STDOUT to OUTPUT: $!\n";
open (ERROR, ">&STDERR") # error sent directly
or die "Can't dup STDERR to ERROR: $!\n";
eval {
$pid = open3(\*INPUT, \*OUTPUT, \*ERROR, $pgp_command)
or die "could not run pgp command : $pgp_command : $!";
print INPUT "\n";
# wait for child to terminate with PID -1 but not in non-blocking m
+ode?
$pid2 = waitpid(-1,0);
};
$@ && die "ERROR: $@\n"; # EVAL_ERROR
print "process id : " . $pid . "\n";
print "exit value \$? : " . $? . "\n";
print "pid2 : " . $pid2 . "\n";
@output = <OUTPUT>
or @errors = <ERROR>
or die "Could not get response from PGP command : $pgp_command :
+$!";
foreach (@output) { print "output " . $_ . "\n"};
foreach (@errors) { print "error " . $_ . "\n"};
close INPUT;
close OUTPUT;
close ERROR;
Can you help? Thanks,
Andrew
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.