如何在Bash脚本中使用Expect [英] How to use Expect in a Bash script

查看:386
本文介绍了如何在Bash脚本中使用Expect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本从Git存储库中提取软件的最新版本并更新配置文件.从存储库中提取时,我必须输入密码.我希望脚本使所有内容自动化,所以我需要它来自动为我填写.我发现这个网站解释了如何使用Expect查找密码提示并发送密码.我还是无法正常工作.

I am trying to write a script that pulls the latest version of my software from a Git repository and updates the configuration files. When pulling from the repository 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.

这是我的剧本:

#!/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/security/expect-ssh-login-script/

Here's the site I got it from: http://bash.cyberciti.biz/security/expect-ssh-login-script/

推荐答案

这是从注释中获得的,并有一些我自己的观察.但是似乎没有人愿意对此提供真正的答案,所以去了:

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:

您的问题是您拥有一个 Expect 脚本,并且将其视为 Bash 脚本. Expect不知道cdcpgit的含义.巴什做.您需要一个可以调用Expect的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. You want 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 password prompt
  expect "*?assword:*"
  # Send password aka $password
  send -- "$password\r"
  # Send blank line (\r) to make sure we get back to the 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密钥可能会更好.然后,您可以完全摆脱expect调用.

However, as larsks pointed out in the comments, you might be better off using SSH keys. Then you could get rid of the expect call altogether.

这篇关于如何在Bash脚本中使用Expect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆