bashdebianexpectvnc-servertightvnc

Set up TightVNC programmatically with BASH


I'm writing a script to set up VNC (amongst other things) on many debian based devices. I want to include VNC in this setup (specifically, tightVNC if possible) and have it set a given password (randomly generated by the script). The problem is, every guide I find seems to assume that a human is doing this, and is ready to sit and type in the password and press enter. I can't seem to get Bash to echo a password to VNC (it always says 'password too short') nor can I get 'expect' to work properly.

An example guide I found looks like this: http://www.penguintutor.com/linux/tightvnc

I'm looking for something similar to this:

#!/bin/bash
echo "Going to configure VNC"
#turn on vnc server
tightvncserver
#spit out password to vnc server for first run only
echo $password
#confirm the pw
echo $password

But, on every virginal run of tightvncserver it always asks for a password to be inputted by hand:

Going to configure VNC

You will require a password to access your desktops.

Password: Password too short

How can I #1 get around this, or #2 use bash / expect to GIVE it a password to make it happy?


Solution

  • # Configure VNC password
    umask 0077                                        # use safe default permissions
    mkdir -p "$HOME/.vnc"                             # create config directory
    chmod go-rwx "$HOME/.vnc"                         # enforce safe permissions
    vncpasswd -f <<<"$password" >"$HOME/.vnc/passwd"  # generate and write a password
    

    Modify to taste, if your packaging for tightvnc uses a location other than ~/.vnc/ for the passwd file.


    If you have separate view-only and full-control passwords, then:

    vncpasswd -f <<<"$full_password"$'\n'"$view_password" >"$HOME/.vnc/passwd"
    

    If you needed compatibility with /bin/sh (or otherwise weren't using #!/bin/bash shebangs), this would instead be:

    vncpasswd -f >"$HOME/.vnc/passwd" <<EOF
    $full_password
    $view_password
    EOF