12

I'm trying to SSH few servers and trying to get sudo -l output of each server.

Below is the script I'm executing

#!/bin/bash
serverlist="/tmp/servers"

while IFS=, read -r server netgroup username user
do
        ssh -tt -q root@$server sudo -U $username -l < /dev/null
done < "$serverlist"

I have found that -tt option in this script as the cause of this error. Any thought on this?

Also i have noted that I don't see this error when i execute below command just for 1 server.

ssh -tt -q root@myserver sudo -U cham01 -l

Below is the complete error message I'am getting: tcgetattr: Inappropriate ioctl for device

Chamara Keragala
  • 5,627
  • 10
  • 40
  • 58
  • Please show the _complete and unedited_ text of the error messages, not just the "Inappropriate ioctl for device" part. There are several different potential causes of this problem and I can't tell which without further information. Also, please tell us whether the script actually _fails_, or if it just prints the output you want but _also_ this error message. – zwol Mar 27 '17 at 13:02
  • 1
    Add `-vvv` switches to the `ssh` command to get more information and the context of the message (instead of the `-q`, which suppress all the information). – Jakuje Mar 27 '17 at 13:45
  • (1) Do you get this error when you do `ssh -tt -q root@myserver sudo -U cham01 -l < /dev/null` ? – zwol Mar 28 '17 at 19:31
  • (2) I repeat: please tell us whether the script actually _fails_, or if it just prints the output you want but also this error message. – zwol Mar 28 '17 at 19:32
  • @zwol Script does not fail. It just prints `tcgetattr: Inappropriate ioctl for device` in the output. Yes i get this error when i do `ssh -tt -q root@myserver sudo -U cham01 -l < /dev/null` – Chamara Keragala Mar 29 '17 at 08:51
  • OK, I think I know what's going on, but one last question. What does `ssh -tt -q root@myserver tty < /dev/null` print? – zwol Mar 29 '17 at 11:47
  • @zwol prints nothing :) – Chamara Keragala Mar 30 '17 at 04:26
  • That's bizarre. Try `ssh -tt -q root@myserver stty < /dev/null` instead, please. Also please tell me the operating system running on the server (ideally, the output of `ssh root@myserver uname -a`). – zwol Mar 30 '17 at 13:01
  • @zwol got following `tcgetattr: Inappropriate ioctl for device speed 38400 baud; line = 0; -brkint -imaxbel` and uname -a is`Linux myserver 2.6.32-642.1.1.el6.x86_64 #1 SMP Tue May 31 11:37:28 PDT 2016 x86_64 x86_64 x86_64 GNU/Linux` – Chamara Keragala Mar 31 '17 at 05:48

3 Answers3

12

tcgetattr: Inappropriate ioctl for device normally means that some program attempted to do a terminal control operation but its standard I/O streams weren't connected to a terminal. (I know this because tcgetattr is the name of a C library function that does terminal control operations.)

Now, the whole point of the -tt option to ssh is to guarantee that the program run on the remote host is connected to a terminal, and stty printing out speed 38400 baud; line = 0; -brkint -imaxbel demonstrates that it was. This is what I get when I run these commands with my servers:

$ ssh myserver stty < /dev/null
stty: 'standard input': Inappropriate ioctl for device

$ ssh -tt myserver stty < /dev/null
speed 38400 baud; line = 0;
-brkint -imaxbel
Connection to myserver closed.

But what you are getting is

$ ssh -tt yourserver stty < /dev/null
tcsetattr: Inappropriate ioctl for device
speed 38400 baud; line = 0;
-brkint -imaxbel

The tcsetattr error is not coming from stty. First something tried to do something terminal-related and failed, and then stty ran successfully. This suggests a bug in your shell startup scripts, which are doing something that is inappropriate when run "non-interactively", causing you to get this error even though you are running commands connected to a terminal. I can't help you any further, but perhaps this old answer about a similar problem offers some clues.

zwol
  • 135,547
  • 38
  • 252
  • 361
4
export GPG_TTY=$(tty)

From: https://github.com/keybase/keybase-issues/issues/2798#issue-205008630

:)

Felipe
  • 16,649
  • 11
  • 68
  • 92
  • This does not work for me. I have tried both on the local machine and on the server, but still get the error. – Tropilio Mar 22 '23 at 11:45
3

In this answer I will not propose a solution (I don't know where the error comes from) but, instead, I am going to suggest a powerful instrument to find it!

To understand where the problem comes from you can use the command strace:

strace ssh -tt -q root@myserver sudo -U cham01 -l < /dev/null 

Sure you will realize which one is the system call that causes the error. The shell will prompt all the system calls and, at some point close to the end,you will see something like:

....
ioctl(3, SNDCTL_TMR_START or TCSETS, {B0 -opost -isig -icanon -echo ...}) = -1 ENOTTY (Inappropriate ioctl for device)
....

Here, you can find examples on how to use it.

Suggestion: before the ioctl(...) system call, there should be an open(...) system call for the same device. Go in the header file of the device and try to have a look on the different command you can pass it. The problem should be a not recognized command (due maybe to an old version of the device driver used). This is just a suggestion.

Leos313
  • 5,152
  • 6
  • 40
  • 69
  • 1
    This is a good technique to know, but it won't help if the error message is coming from the remote end of the `ssh` connection. – zwol Sep 15 '20 at 20:06
  • right! I just wanted to share the way I found a problem some years ago! It may be helpful for someone I guess – Leos313 Sep 16 '20 at 08:09