21

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.)

4

3 Answers 3

7

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
2
  • I have a related error but don't understand the actual error msg: Password for brando9: stty: 'standard input': Inappropriate ioctl for device, what does it mean? Nov 25, 2022 at 19:23
  • I can't use sudo :( Nov 25, 2022 at 20:11
5

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).

17
  • 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.
    – MountainX
    Jul 11, 2013 at 23:25
  • @MountainX Right, sorry, I'd misread the question. Here's an expect script (completely untested). Jul 11, 2013 at 23:45
  • 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.
    – MountainX
    Jul 12, 2013 at 0:56
  • 1
    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.
    – MountainX
    Jul 12, 2013 at 1:07
  • 1
    @CharlieParker Ask this as a question. (And I personally wouldn't know, I'm not familiar with this reauth command (is that a standard thing of AFS? If not you'll need to explain where it's from).) Nov 25, 2022 at 19:50
1

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
1
  • 1
    Thanks. However, the error is not coming from sudo it is coming from x11vnc -storepassword.
    – MountainX
    Jul 11, 2013 at 23:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .