我正在尝试编写autobuild脚本。在构建命令结束时,系统会提示用户输入密码,我想自动输入密码。我目前所拥有的是
#!/bin/bash
##stored username and password
userName = [username]
password = [password]
##connect to build server
ssh ${username]@xx.x.xx.xx
##checkout copy from svn
svn co [path of code]
##change directory to build directory
cd [build directory of checked out code]
##start build
##expect password
/usr/bin/expect << EOF
expect "Password: "
spawn make build
send ${password}
EOF
exit
echo "Build Complete"
我展示了另一种在bash脚本中执行期望的方式
expect -c \
"set timeout -1; \
spawn make build; \
expect \"password: \"; \
send -- \[password]\r\"; \
expect eof"
在第二个例子中,[password]是一个需要密码的字符串。
当build命令提示输入密码时,它会立即生效。我尝试过其他几个例子,而spawn似乎根本不起作用。
#!/usr/bin/expect
expect "hello"
spawn echo "hello"
send "world"
直到我输入“你好”
才能做什么任何帮助将不胜感激!
答案 0 :(得分:1)
正如@chepner评论的那样,你的逻辑是错误的:你在本地机器上做了大部分工作,而不是远程工作。您需要这样的东西才能连接到远程服务器并与那里的登录shell进行交互:
#!/usr/bin/expect -f
##stored username and password
set userName "username"
set password "password"
set prompt "pattern to match user's prompt"
##connect to build server
spawn ssh $username@xx.x.xx.xx
expect -re $prompt
##checkout copy from svn
send "svn co [path of code]\r"
expect -re $prompt
##change directory to build directory
send "cd [build directory of checked out code]\r"
expect -re $prompt
##start build
set timeout -1 ; # wait as long as required for build to finish
send "make build\r"
expect "Password: "
send -- "$password\r"
# get a prompt when build complete
expect -re $prompt
send "exit\r"
expect eof
puts "Build Complete"