简体   繁体   English

为需要密码并输入密码的 Windows 编写 ActiveState Tcl 程序

[英]Write ActiveState Tcl Program for Windows that expects a password and enters it

I want to write a shell program in windows that runs another shell script and expects a password prompt from the Git bash terminal and inputs it.我想在 Windows 中编写一个 shell 程序,该程序运行另一个 shell 脚本并期望来自 Git bash 终端的密码提示并输入它。 This is what I have so far:这是我到目前为止:

#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
package require Expect

spawn sampleScript.sh
expect "Password:"
send "pass123"

sampleScript.sh code: sampleScript.sh 代码:

echo 'Hello, world.' >foo.txt

my program outputs the following:我的程序输出如下:

'The operation completed successfully. while executing "spawn sampleScript.sh" 
(file "compare.tcl" line 6)'

However, there is no foo.txt that is created in my local file folder where the scripts are.但是,在脚本所在的本地文件夹中没有创建 foo.txt。 Can you help?你能帮我吗?

The key with expect programs is to let the spawned program exit gracefully.期望程序的关键是让生成的程序优雅地退出。 As it currently stands, after your expect script sends the password, it immediately exits, and that kills the spawned program too early.按照目前的情况,在您的期望脚本发送密码后,它会立即退出,这会过早地杀死生成的程序。

  • If you don't need to interact with the sampleScript (ie just let it run to completion), the last line in the expect script should be如果您不需要与 sampleScript 交互(即让它运行到完成),则期望脚本中的最后一行应该是

    expect eof
  • Otherwise, use否则,使用

    interact

Read How to create a Minimal, Reproducible Example -- your updated code does not reproduce the error you're seeing阅读如何创建最小的、可重现的示例——您更新的代码不会重现您看到的错误

  • Tcl code:代码:
    1. when you send something, you usually need to "hit Enter": send "password\\r"当你发送东西时,你通常需要“回车”: send "password\\r"
    2. Did you add expect eof to the Tcl script?您是否在 Tcl 脚本中添加了expect eof If not, you might be killing sampleScript.sh before it has a chance to create the output file如果没有,您可能会在它有机会创建输出文件之前杀死 sampleScript.sh
  • sampleScript.sh: Is that really your sample script? sampleScript.sh:这真的是你的示例脚本吗? Where's the password prompt?密码提示在哪里?

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

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