使用bash脚本期望 [英] using expect in bash script
问题描述
我想写,从git仓库拉最新版本我的软件和更新配置文件的脚本。当从回购拉虽然,我必须输入密码。我希望脚本自动一切,所以我需要它自动填充它适合我。我发现这个网站,介绍了如何使用期望寻找密码的提示,并发送密码。我不能让它工作,虽然。这里是我的脚本:
I am trying to write a script that pulls the latest version of my software from a git repo and updates the config files. When pulling from the repo though, i have to enter a password. I want the script to automate everything, so I need it to automatically fill it in for me. I found this site that explained how to use "expect" to look for the password prompt and send the password. I can't get it to work though. Here's my script:
#!/usr/bin/expect -f
set password [lrange $argv 0 0]
set timeout -1
clear
echo "Updating Source..."
cd sourcedest
git pull -f origin master
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
git checkout -f master
cp Config/database.php.bak Config/database.php
cp webroot/index.php.bak webroot/index.php
cp webroot/js/config.js.bak webroot/js/config.js
我是什么做错了吗?这里的网站我是从: http://bash.cyberciti.biz/安全/预计-SSH登录脚本/
推荐答案
这是pretty从注释中多取,用我自己的一些看法。但似乎没有人想提供一个真正的答案,所以这里有云:
This is pretty much taken from the comments, with a few observations of my own. But nobody seems to want to provide a real answer to this, so here goes:
您的问题是你有一个的期望的脚本,你喜欢的庆典的脚本处理它。期待不知道是什么 CD
, CP
和混帐
的意思。击一样。你想要的是一个bash脚本,使得调用期待。例如:
Your problem is you have an expect script and you're treating it like a bash script. Expect doesn't know what cd
, cp
, and git
mean. Bash does. What you want is a bash script that makes a call to expect. For example:
#!/usr/bin/env bash
password="$1"
sourcedest="path/to/sourcedest"
cd $sourcedest
echo "Updating Source..."
expect <<- DONE
set timeout -1
spawn git pull -f origin master
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
DONE
git checkout -f master
cp Config/database.php.bak Config/database.php
cp webroot/index.php.bak webroot/index.php
cp webroot/js/config.js.bak webroot/js/config.js
不过,由于larsks在评论中指出的那样,你可以使用SSH密钥会更好。然后,你可以摆脱期望
干脆叫
这篇关于使用bash脚本期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!