gpt4 book ai didi

linux - 如何将 bash 中的值传递到 Expect 中?

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:06 29 4
gpt4 key购买 nike

我有一个非常简单的bash脚本,需要expect。我需要将 bash 中的值传递到 Expect 中,并且我不会尝试 ssh 到另一台服务器或任何其他东西(因为我似乎只找到有关通过 ssh 登录另一台服务器的问题)。

这个想法很简单:

#!/usr/bin/env bash

echo "Please enter your password: "
read PASSWD

x=$(expect -c '
spawn su -c 'whoami'
expect "Password:"
send "$PASSWD\r"
interact
')

所以这不起作用。 Expect shell 无法识别 $PASSWD 变量。如何实现这一点?

谢谢。

最佳答案

另一种选择是将 PASSWD 存储在环境中并让 Expect 在那里获取它:

read -p "Your password: " passwd
export passwd

expect -c '... ; send "$env(passwd)\r"; ...'

从安全角度来看,最好的选择可能是期望提示输入密码:然后,密码将不会显示在命令行上或环境中。

expect -c <<'END'
stty -echo
send_user "Your password: "
expect_user -re "(.*)\n"
send_user "\n"
set passwd $expect_out(1,string)
stty echo

# your script starts here
...
send "$passwd\r"
...
END

关于linux - 如何将 bash 中的值传递到 Expect 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326125/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com