需要配置许多VNC服务器,用脚本能够自动执行大部分操作,不过首次配置需设置vnc密码,略为苦恼。
expect是Linux中交互的好工具。
#!/usr/bin/expect #第一行必不可少,表明调用expect set timeout 10 spawn vncpasswd #spawn是expect中的命令,调用vncpasswd命令 expect "Password:" #expect当遇到“Password”时 send "123456\n" #送入“123456”并回车,windows中是\r\n,linux中只需\n expect "Verify:" send "123456\n" interact #返回终端
如果要使用echo 把它写入一个文件中,则应当注意对 !号 "号的处理
输出!号 需要用 echo ''单引号
输出" 号 需要用 \" 进行转义
echo '#!/usr/bin/expect' >> /root/vncpasswdshell.sh echo "set timeout 10" >> /root/vncpasswdshell.sh echo "spawn vncpasswd" >> /root/vncpasswdshell.sh echo "expect \"Password:\"" >> /root/vncpasswdshell.sh echo "send \"123456\n\"" >> /root/vncpasswdshell.sh echo "expect \"Verify:\"" >> /root/vncpasswdshell.sh echo "send \"123456\n\"" >> /root/vncpasswdshell.sh echo "interact" >> /root/vncpasswdshell.sh chmod +x /root/vncpasswdshell.sh cd /root ./vncpasswdshell.sh
interact会返回交互状态,从而使接下来的命令暂停执行。
如果想一路顺下去执行。可以采用expect eof;exit
从而退出!#/usr/bin/expect脚本