Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

' Inappropriate ioctl for device ' on read from SSH over open2() handle

by jblank (Initiate)
on Jan 28, 2008 at 21:44 UTC ( #664789=perlquestion: print w/ replies, xml ) Need Help??
jblank has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on ' Inappropriate ioctl for device ' on read from SSH over open2() handle
Download Code
Re: ' Inappropriate ioctl for device ' on read from SSH over open2() handle
by Joost (Canon) on Jan 28, 2008 at 23:06 UTC
    Just as a possible way to work around this, you could give the remote user sudo permissions without requiring a password for certain commands, which will probably fix the requirement for a tty, or you could give the remote user higher level access but restrict the login shell to do only what you want to do. See the sshd and sudoers documentation on your system.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664789]
Approved by Errto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2014-12-18 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (47 votes), past polls