bash script error stty: standard input: Inappropriate ioctl for device

95,627

Solution 1

x11vnc expects its standard input to be a terminal, and it changes the terminal mode to avoid echoing the password as you're typing. When standard input isn't a terminal, the stty calls to turn echo off and back on fail, hence the warning that you see.

Expect is indeed a solution. Try this script (untested):

#!/usr/bin/expect -f
spawn x11vnc -storepasswd ~/.vnc/passwd
expect "password:" {send "swordfish" "\r"}
expect "password:" {send "swordfish" "\r"}
expect "Write*\?" {send "y\r"}

Alternatively, if you can, use an authentication method other than RFB (-passwdfile, or an SSL client certificate).

Solution 2

Another option to avoid those warning messages is to execute x11vnc in a pseudo-terminal created by a UNIX command (see Using pseudo-terminals (pty) to control interactive programs). This can be done with the script command or tools such as pdip ("Programmed Dialogue with Interactive Programs").

The warning messages on Mac OS X 10.6.8 for not providing a pseudo-terminal for x11vnc:

# x11vnc 0.9.14
sudo x11vnc -storepasswd ~/.vnc/passwd << ENDDOC
password
password
y
ENDDOC

# Enter VNC password: stty: stdin isn't a terminal
#
# Verify password:    
# stty: stdin isn't a terminal
# Write password to ~/.vnc/passwd?  [y]/n Password written to: ~/.vnc/passwd

Solutions using the script command:

# GNU script command
sudo script -q -c 'x11vnc -storepasswd ~/.vnc/passwd' <<ENDDOC /dev/null
password
password
y
ENDDOC

# ... or ...
printf '%s\n' 'password' 'password' 'y' | 
   sudo script -q -c 'x11vnc -storepasswd ~/.vnc/passwd' /dev/null


# FreeBSD script command
sudo script -q /dev/null x11vnc -storepasswd ~/.vnc/passwd <<ENDDOC
password
password
y
ENDDOC

Solution 3

Sudo has an option -S that allows it to read the passwd from STDIN.

[user@evil ~]$ tail -1 /etc/shadow
tail: cannot open `/etc/shadow' for reading: Permission denied
[user@evil ~]$ echo 'P@ssW3rd!' | sudo -S tail -1 /etc/shadow
nfsnobody:!!:15891::::::

Here is an example script to demonstrate the process:

#!/bin/bash

function hr {
    perl -e 'print "-" x 80, "\n";'
}

hr
read -p "Please enter your sudo password: " -s sudopasswd
echo

hr
echo "-sudo run: tail -1 /etc/shadow"
tail -1 /etc/shadow

hr
echo "+sudo run: tail -1 /etc/shadow"
echo "$sudopasswd" | sudo -S tail -1 /etc/shadow

hr
echo "-sudo run: ls -la /root/"
ls -la /root/

hr
echo "+sudo run: ls -la /root/"
echo "$sudopasswd" | sudo -S ls -la /root/

hr

Your script, would simply need to do something like:

read -p "Please enter your sudo password: " -s sudopasswd
echo "$sudopasswd" | sudo -S x11vnc -storepasswd ~/.vnc/passwd 

This would allow you to use sudo commands in your script without having to hardcode a password.

Alternatively, you could add your user, or a subset of users, the capability to run x11vnc with sudo, without a password, but adding a line like this to /etc/sudoers:

user    ALL=(root) NOPASSWD: /path/to/x11vnc

Or create a vncusers group, add users to that group, and add the following to /etc/sudoers:

%vncusers    ALL=(root) NOPASSWD: /path/to/x11vnc
Share:
95,627

Related videos on Youtube

Jarek
Author by

Jarek

You may be interested in the story of SE moderator Monica Cellio and how she was unfairly treated by the corporate management of this site. More info here. An update is available. Let's hope we can cultivate a more fair environment for content creators and moderators going forward.

Updated on September 18, 2022

Comments

  • Jarek
    Jarek over 1 year

    I'm using here-documents in a bash script to automate installation and setup where a password is required many times. I enter the password once and the script passes it to the various commands. In most instances the here-document approach handles this fine. However, in one case I get this error:

    Enter VNC password: stty: standard input: Inappropriate ioctl for device
    Verify password:    
    stty: standard input: Inappropriate ioctl for device
    

    Please notice that this error message is from x11vnc -storepassword (not from sudo.)

    My problem is related to x11vnc -storepasswd and here's my code:

    sudo x11vnc -storepasswd ~/.vnc/passwd << ENDDOC
    password
    password
    y
    ENDDOC
    

    That obviously (from the error) does not work. I would appreciate a working example of how to implement sudo x11vnc -storepasswd ~/.vnc/passwd in a script.

    In case it helps, the prompts look like this:

    Enter VNC password:
    Verify password:
    Write password to /home/user/.vnc/passwd? [y]/n n

    Will using expect be a better solution? If so, how would I use it in this case? (I have never used expect before but I have looked at a lot of examples since posting this question and I cannot get expect to work on my own.)

  • Jarek
    Jarek almost 11 years
    Thanks. However, the error is not coming from sudo it is coming from x11vnc -storepassword.
  • Jarek
    Jarek almost 11 years
    Thanks. However, the error is not coming from sudo it is coming from x11vnc -storepassword. I've been trying various expect approaches and I can't seem to get it right. An example using expect to enter a password for x11vnc -storepassword would be greatly appreciated. I'll update my question to avoid further confusion.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 11 years
    @MountainX Right, sorry, I'd misread the question. Here's an expect script (completely untested).
  • Jarek
    Jarek almost 11 years
    Thank you. Your untested script gave me a few additional clues, but ultimately it also does not run without errors. The error is simply Enter VNC password: usage: send [args] string on the line expect "password:" {send "swordfish" "\r"}. I'm not sure how to fix that. Expect seems to be a very picky tool because I've been fooling with this particular problem for hours without working results yet.
  • Jarek
    Jarek almost 11 years
    The error (above comment) was coming from send "swordfish" "\r" and it was resolved by send "swordfish\r". However, the solution still doesn't work. No password is written to ~/.vnc/passwd. I am still clueless as to why. As I said, I've been seeing this result in spite of trying everything I can think of so far.
  • Jarek
    Jarek almost 11 years
    BTW, the same commands used in your expect solution work when entered manually. They do not work in this expect script or any variation of it that I have tried so far.
  • alper
    alper over 3 years
    I am getting following error: spawn: command not found, line 3: expect: command not found should I put it into .zshrc file?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 3 years
    @alper This an an expect script, not a shell script. You need to write the code to a file, make it executable, and execute it. And you need to install expect.