加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash脚本中使用expect

发布时间:2020-12-15 18:58:50 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个脚本,从git repo中提取我的软件的最新版本并更新配置文件.从回购中提取时,我必须输入密码.我希望脚本能够自动化所有内容,所以我需要它来自动填充它.我发现这个网站解释了如何使用“expect”来查找密码提示并发送密码.我不能让它工作.这是
我正在尝试编写一个脚本,从git repo中提取我的软件的最新版本并更新配置文件.从回购中提取时,我必须输入密码.我希望脚本能够自动化所有内容,所以我需要它来自动填充它.我发现这个网站解释了如何使用“expect”来查找密码提示并发送密码.我不能让它工作.这是我的脚本:
#!/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 -- "$passwordr"
# 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/security/expect-ssh-login-script/

这几乎是从评论中得出的,我自己也有一些观察.但似乎没有人愿意为此提供真正的答案,所以这里有:

您的问题是您有一个期望脚本,并且您将其视为bash脚本.期望不知道cd,cp和git是什么意思.巴什呢.你想要的是一个调用期望的bash脚本.例如:

#!/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 -- "$passwordr"
  # 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密钥可能会更好.然后你可以完全摆脱期待的呼唤.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读