期待脚本 return "missing close-bracket"

Expect script return "missing close-bracket"

我编写了一个 bash 脚本,它从 API 获取密码并使用 "send" 设置此密码

un=admin
pw=$(get_json_value_from_file "$rsm_res_file" "password")
echo $pw
export HISTIGNORE="expect*";
expect -c "
    spawn ssh -o UserKnownHostsFile=$ap_known_hosts_file -p $PORT  $un@localhost
    expect "*ogin*"
    send \"$un\r\"
    expect "*assword*"
    send \"$pw\r\"
    interact"

它重新调整了以下异常:

  missing close-bracket
while executing "send \"f40T[2[6g%^TsMLv\r\"
    interact"

我认为是特殊字符的问题。

您的密码包含一个具有特殊含义的 [ 字符。 Tcl/Expect 中的 [...] 语法是命令替换,就像 Bash 中的 `...`$(...) 一样。要获得文字 [,您必须使用 \[。对于您的场景,最简单的解决方案是使用环境变量将密码从 Bash 传递到 Expect。例如:

PASSWORD=$pw expect -c "
    spawn ssh -o UserKnownHostsFile=$ap_known_hosts_file -p $PORT  $un@localhost
    expect "*ogin*"
    send \"$un\r\"
    expect "*assword*"
    send $env(PASSWORD)\r
    interact"

export PASSWORD=$pw
expect -c "
    spawn ssh -o UserKnownHostsFile=$ap_known_hosts_file -p $PORT  $un@localhost
    expect "*ogin*"
    send \"$un\r\"
    expect "*assword*"
    send $env(PASSWORD)\r
    interact"