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 "$PWDr"
expect "Verify:"
send "$PWDr"
expect "Would you like to enter a view-only password"
send "nr"
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 "$PWDr"
expect "Verify:"
send "$PWDr"
expect "Would you like to enter a view-only password"
send "nr"
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 "$PWDr"
expect "Verify:"
send "$PWDr"
expect "Would you like to enter a view-only password"
send "nr"
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 "$PWDr"
expect "Verify:"
send "$PWDr"
expect "Would you like to enter a view-only password"
send "nr"
sleep 10
expect eof
sleep 10
EOF
That didn’t work either…
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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 "$PWDr"
expect "Verify:"
send "$PWDr"
expect "Would you like to enter a view-only password"
send "nr"
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 :1r"
expect "Password:"
send "$PWDr"
expect "Verify:"
send "$PWDr"
expect "Would you like to enter a view-only password"
send "nr"
expect eof
EOF
Worked.
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