Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
    
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 '13 at 23:25
    
@MountainX Right, sorry, I'd misread the question. Here's an expect script (completely untested). –  Gilles Jul 11 '13 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 '13 at 0:56
    
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. –  MountainX Jul 12 '13 at 1:04
    
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 '13 at 1:07
add comment

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|improve this answer
    
Thanks. However, the error is not coming from sudo it is coming from x11vnc -storepassword. –  MountainX Jul 11 '13 at 23:23
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.