运行 bash 期望脚本中的脚本

Run bash script inside the expect script

我正在尝试 运行 我的 bash 脚本在 expect 脚本中,但出现错误。

/usr/bin/expect <<EOD
spawn ssh nginubud@10.123.25.83 $(< try1.sh)
expect "assword:"
send "$reg\r"
expect eof
EOD

我正在尝试在 expect ssh nginubud@10.123.25.83 "$(< try1.sh)" 中执行此操作,这个正在运行,但我需要找到一种以自动化方式 运行 它的方法。我不想使用 RSA 密钥。

遇到的错误:

spawn ssh nginubud@10.123.25.83 #tats script
invalid command name "echo"
    while executing
"echo "Enter Year:""

我也可以 运行 我期望的 ssh 脚本但是当我包含并尝试 运行 我的 $(< try1.sh) 我得到 "no variable errors"

您可以使用 ssh user@host bash -c ...。例如:

[bash] % cat foo.sh
export CMD=$( printf '%q' "$(< try.sh)" )
expect << EOF
spawn ssh foo@localhost bash -c $::env(CMD)
expect -nocase password:
send bar\r
expect eof
EOF
[bash] % cat try.sh
echo hello world | tr a-z A-Z
[bash] % bash foo.sh
spawn ssh foo@localhost bash -c echo\ hello\ world\ \|\ tr\ a-z\ A-Z
foo@localhost's password:
HELLO WORLD
[bash] %