繁体   English   中英

期待在 Bash 脚本中

[英]Expect In Bash Script

我正在尝试编写一个自动构建脚本。 在构建命令结束时,系统会提示用户输入密码,我想自动发送密码。 我目前拥有的是

#!/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] 实际上是一串所需的密码。

当构建命令提示输入密码时,它会继续存在。 我尝试了其他一些示例,但 spawn 似乎根本不起作用,例如

#!/usr/bin/expect
expect "hello"
spawn echo "hello"
send "world"

在我输入“你好”之前什么都不做

任何帮助将非常感激!

正如@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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM