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";
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.