期望调用 shell 脚本

Expect call shell script

我正在尝试调用 shell 脚本并将结果存储在 expect 变量中。 get_pw.sh 接受 2 个参数并使用提供的 md5hash 解密文件。如果我从 bash 提示执行 ./get_pw.sh file.test md5hash,它会按预期 returns 密码字符串。从 expect 调用时,不会返回密码。预期调试显示:

expect: does "" (spawn_id exp0) match regular expression "[^\s]"?

所以看起来脚本在从 expect 调用时没有返回密码字符串。相关代码:

#!/usr/bin/expect
send "./get_pw.sh file.test md5hash \r"
expect -re {[^\s]} {
set password $expect_out(0,string)
}
puts "The password is: $password"

您需要先spawn一个命令,然后才能send输入并从中expect输出。

要将 expect 变量设置为命令的输出,请使用

set varname [exec command]

如果你必须使用 expect,

  log_user 0
  spawn -noecho get_pw.sh file hash
  expect eof
  set passwd [string trimright $expect_out(buffer) "\r\n"]
  puts $passwd

Jens 的回答现在看起来不错...