I am trying to run a program from a perl script. I redirect STDOUT and STDERR to two different files. The program I am running asks for a password that I try to print to the process handle but that doesn't work as the program immediately prints an authentication error warning me that I didn't write anything to STDIN.
The code I am using is similar to this:
my $s = qq(some_program > someprogram.out 2> someprogram.err);
open(my $f, "|$s") or die "Couldn't run program: $! $?";
# print `cat someprogram.out`; # An error has already been printed here!
print $f "password\n";
close $f or die "$!: $?";
I have run this in the perl debugger and noticed that when the debugger runs the open line, I can run the commented out print line and the error is already in the output file.
So, what am I forgetting to do in order for this to work? How do I tell open to "wait for STDIN"?
---UPDATE!
I did a test like this in the command line:
echo password | some_program
and the authentication error was printed. So it looks like some_program is really not reading from STDIN but from tty.
Is there a way to print to tty so the program can read the password from there in Perl?
some_program
is reading the password from standard input? Ifsome_program
is ssh, to give one of many examples, it is probably trying to read from the terminal, which is not necessarily the same thing as standard input. - mobsome_program
is not reading from STDIN and doing it from the terminal; not that it was ssh. I did a test like this in the command line:echo password | some_program
and the authentication error appears. So the program is not reading from STDIN. - Ivan Salazar