Automating vncserver with expect not working as expected

I thought of automating the part when i first run vncserver, since it ask for a password (and while i know there a flag to pass a passwd file, i prefer to generate a new one every time i need/want to):

#!/bin/bash

read -s PWD

/usr/bin/expect -d <<EOF
spawn /usr/bin/vncserver -geometry 1366x768 :1
expect "Password:"
send "$PWD\r"
expect "Verify:"
send "$PWD\r"
expect "Would you like to enter a view-only password"
send "n\r"
expect eof
EOF

Here i use the -d flag to see the debugging output, it seems that the glob does match with the expect part, at least the second time (fail once, then match after the second try).

It also work in sending the password (here i used the example password "isthisevenworking") which seems to work.

vncserver also show the usual output when it work, BUT, when looking at running process, vncserver isn’t running…(although there is a passwd file in the .vnc folder)

Though, vnc does work with the example password if i run it manually after the script end, or if i add the same command at the end of the script like so:

#!/bin/bash

read -s PWD

/usr/bin/expect -d <<EOF
spawn /usr/bin/vncserver -geometry 1366x768 :1
expect "Password:"
send "$PWD\r"
expect "Verify:"
send "$PWD\r"
expect "Would you like to enter a view-only password"
send "n\r"
expect eof
EOF
/usr/bin/vncserver -geometry 1366x768 :1

What I’m wondering is: Why isn’t the vncserver command running after the expect part end, even though it does if it run outside the expect script?

I don’t mind adding it at the end of the script as it work, but it feel a bit unnecessary given it would run anyway after setting the password outside of expect…

EDIT: I think i know why this doesn’t work as "expected". Seems like the script stop too soon and stop vncserver before it launch itself. the same can be emulated manually by doing:

/usr/bin/vncserver -geometry 1366x768 :1

entering the password two time, entering n then doing Ctrl+D right after.

So i thought of adding either infinite timeout:

#!/bin/bash

read -s PWD

/usr/bin/expect -d <<EOF
set timeout -1
spawn /usr/bin/vncserver -geometry 1366x768 :1
expect "Password:"
send "$PWD\r"
expect "Verify:"
send "$PWD\r"
expect "Would you like to enter a view-only password"
send "n\r"
expect eof
EOF

but that didn’t work still. Lastly tried sleep:

#!/bin/bash

read -s PWD

/usr/bin/expect -d <<EOF
spawn /usr/bin/vncserver -geometry 1366x768 :1
expect "Password:"
send "$PWD\r"
expect "Verify:"
send "$PWD\r"
expect "Would you like to enter a view-only password"
send "n\r"
sleep 10
expect eof
sleep 10
EOF

That didn’t work either…

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Seems like this post is related to my problem.
So i went and tried the two answer/method there:

#!/bin/bash

read -s PWD

/usr/bin/expect -d <<EOF
set timeout -1
spawn -ignore HUP /usr/bin/vncserver -geometry 1366x768 :1
expect "Password:"
send "$PWD\r"
expect "Verify:"
send "$PWD\r"
expect "Would you like to enter a view-only password"
send "n\r"
expect_background
expect eof
EOF

Didn’t work, but:

#!/bin/bash

read -s PWD

/usr/bin/expect -d <<EOF
set timeout -1
spawn screen bash
send "/usr/bin/vncserver -geometry 1366x768 :1\r"
expect "Password:"
send "$PWD\r"
expect "Verify:"
send "$PWD\r"
expect "Would you like to enter a view-only password"
send "n\r"
expect eof
EOF

Worked.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply