我编写了一个 bash 脚本,它从 API 获取密码并使用“发送”来设置此密码
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"
关于linux - 期望脚本返回 "missing close-bracket",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39994688/