0

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

2
  • 1
    Perhaps this is relevant? Background spawned process in Expect Mar 22 at 0:45
  • Seems like it is indeed, nice catch! Tried the two answer there, only the screen method worked fine. The other one didn't seem to work so i guess that's that. Will post an answer in a bit @steeldriver Mar 22 at 0:57
0

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.

2
  • 1
    With the first script, the expect_background is indeed bogus. You should try removing both it and the expect eof. This should let the script exit, and because of the -ignore HUP, the child process should continue. Of course, this does not stop some other agent killing the process, for example if running from a systemd unit that ends.
    – meuh
    Mar 22 at 10:18
  • 1
    sadly that didn't work :/ Guess only the screen method work for now @meuh Tried it but it still exit too soon for the vncserver process to start (though it does set the passwd right as already mentioned) Mar 22 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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