Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

' 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 = ''; 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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664789]
Approved by Errto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2018-05-23 13:12 GMT
Find Nodes?
    Voting Booth?