简体   繁体   English

使用expect执行shell脚本时出错

[英]Error using expect to execute shell script

I am using an expect script to automatically answer prompts in a shell script designed to add Linux systems to active directory.我正在使用一个期望脚本来自动回答旨在将 Linux 系统添加到活动目录的 shell 脚本中的提示。 I only have three prompts: location, username, password.我只有三个提示:位置,用户名,密码。 I am using Ansible to execute it.我正在使用 Ansible 来执行它。

The issue when the script executes, it tries to install ~ 49 rpms on the vm, adcli, realmd, etc. Yum begins install the rpms but it never fully completes.脚本执行时的问题,它尝试在 vm、adcli、realmd 等上安装 ~ 49 rpms。Yum 开始安装 rpms,但它从未完全完成。 Sometimes, it just stops after 17, 46, or 42 rpms.有时,它会在 17、46 或 42 rpms 后停止。 It doesn't produce an error, but just inconsistently stops and the vm is obviously not joined to AD.它不会产生错误,但只是不一致地停止,并且 vm 显然没有加入 AD。

I know I can use Ansible to write the tasks and sidestep the use of the bash script.我知道我可以使用 Ansible 来编写任务并回避使用 bash 脚本。 My goal was to at least temporarily use the work from another coworker and generate an Ansible play later on.我的目标是至少暂时使用另一位同事的工作并在以后生成 Ansible 游戏。

If I use the script outside of Ansible Tower, it works just fine.如果我在 Ansible Tower 之外使用脚本,它就可以正常工作。 All of the functions in the shell script are executed and rpms are installed as expected. shell 脚本中的所有函数都会按预期执行并安装 rpm。 This is my first foray using expect so I may be doing something incorrectly.这是我第一次尝试使用 expect,所以我可能做错了什么。 Thanks for taking a look.谢谢参观。

  - name: Joining of system to the domain
    ansible.builtin.shell: |
      set timeout 10
      spawn sudo ./addto-AD
      match_max 100000

      expect -exact "Please Enter the Environment (i.e. Portand or Seattle): "
      send -- "Seattle\r"
      expect -exact "\r"

      expect -exact "Enter Elevated AD Username: "
      send -- "{{ elevated }}\r"
      expect -exact "\r"

      expect -exact "Enter Password for User: "
      send -- "{{ elevated_pass }}\r"
      expect -exact "\r"
      
      expect eof

expect eof is still subject to the timeout. expect eof仍然受到超时的影响。 The script is probably taking longer than 10 seconds to complete.该脚本可能需要超过 10 秒才能完成。 I'd suggest set timeout -1我建议set timeout -1


Obviously I don't know what the script is doing, but maybe you don't need expect for this: Try sending the responses to the script's stdin显然我不知道脚本在做什么,但也许你不需要对此期望:尝试将响应发送到脚本的标准输入

printf '%s\n' "Seattle" "{{ elevated }}" "{{ elevated_pass }}" | sudo ./addto-AD

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

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