Hello o monks of Perl knowledge:
I am having an odd problem with reading from SSH via the reader handle provided by open2(). (As you know, open2() opens two handles: One to read and one to write, permitting bidirectional communication.)
As part of an application I am working on, I need the ability to execute a small script with 'sudo' on a remote host via ssh. Sudo, in turn, requires a pseudo-tty. Thus, I must add the '-t -t' flags to SSH to force it to allocate a pseudo-tty. Without these flags, I do not get my mysterious warning/error; however, my script breaks, since sudo cannot run!
The warning/error in question is 'tcgetattr: Inappropriate ioctl for device'. I get this error after I open2() a pipe through an openssh session and attempt to read from it. The read succeeds... but on the first read, I get that error/warning (I'm not quite sure which it would be considered, an error or a warning).
I do not have this problem with open(), nor with open3()-- and since open3() does everything that open2() does, I have migrated my script to open3(). However, I am seeking explanations here. Have I found a bug? Is ssh trying to do something to stderr that open2() cannot handle?
Code snippet below. Set $TESTHOST to the IP of a host that you can SSH to without passwords (via authorized_hosts) and watch the error-y fun! (For some reason, on my machine, I only see the error when I am logged in as a normal user, not as root. Go figure.)
$TESTHOST = '1.2.3.4';
use IPC::Open2;
$SSH_FLAGS = qq^-t -t -q -o "ConnectTimeout=30"
-o "PasswordAuthentication=no" -o "StrictHostKeyChecking=no"
-o "ChallengeResponseAuthentication=no"
-o "PreferredAuthentications=publickey,server"^;
$SSH_FLAGS =~ s/[\n\r\t]/ /g;
$tmpcmd = "ssh ${SSH_FLAGS} ${TESTHOST} ls";
open2(*SSHREAD, *SSHWRITE, $tmpcmd);
while (<SSHREAD>) {
$currline = $_; print $currline;
}
close(SSHREAD); close(SSHWRITE);
print "Done!\n";