使用expect自动化vncserver未按预期工作

使用expect自动化vncserver未按预期工作

当我第一次运行 vncserver 时,我想到了自动化该部分,因为它要求输入密码(虽然我知道有一个标志可以传递 passwd 文件,但我更喜欢每次需要/想要时生成一个新的):

#!/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

这里我使用-d标志来查看调试输出,看起来 glob 确实与期望部分匹配,至少是第二次(失败一次,然后在第二次尝试后匹配)。

它也可以用于发送密码(这里我使用示例密码“isthisevenworking”),这似乎有效。

vncserver 工作时也会显示通常的输出,但是,当查看正在运行的进程时,vncserver 没有运行...(尽管文件夹passwd中有一个文件.vnc

不过,如果我在脚本结束后手动运行 vnc,或者如果我在脚本末尾添加相同的命令,则 vnc 确实可以使用示例密码,如下所示:

#!/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

我想知道的是:为什么 vncserver 命令在期望部分结束后没有运行,即使它在期望脚本之外运行?

我不介意在脚本的末尾添加它,因为它可以工作,但感觉有点不必要,因为它在设置密码超出预期后无论如何都会运行......

编辑:我想我知道为什么这不能按“预期”工作。似乎脚本停止得太快并在启动之前停止 vncserver。可以通过执行以下操作手动模拟相同的内容:

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

输入密码两次,然后输入然后n执行Ctrl+ 。D

所以我想到添加无限超时:

#!/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

但这仍然不起作用。最后尝试过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

那也没用...

答案1

似乎帖子与我的问题有关。所以我去那里尝试了两个答案/方法:

#!/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

没有用,但是:

#!/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

工作了。

相关内容