期望在包含特殊字符的密码中添加大括号
expect adding curly brackets to password containing special characters
我想写一个小的 expect 脚本和另一个 bash 脚本来省去在 ssh 连接中输入密码的工作。
脚本如下:
// ssh.exp, the real workhorse
#!/usr/bin/expect -f
# usage: ./ssh.exp host user pass
set host [lrange $argv 0 0]
set user [lrange $argv 1 1]
set pass [lrange $argv 2 2]
spawn ssh $user@$host
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact
// the bash script to call it
#!/bin/bash
host='my.host.com'
user='someuser'
pass='Qwerty389$'
./ssh.exp $host $user $pass
但是,当测试脚本运行时,ssh服务器总是报错密码。
我尝试转义美元符号,例如 pass='Qwerty389$'
,但无济于事。
将调试语句exp_internal 1
放入expect脚本,显示发送的密码为:
send: sending "{Qwerty389$}\r" to { exp6 } // without escaping $
send: sending "{Qwerty389$}\r" to { exp6 } // escaping $ in password
不确定为什么要将传递给它的密码放在花括号中。我验证了如果密码里没有美元符号,就没有括号。
有什么帮助吗?
shell代码需要引用变量:
./ssh.exp "$host" "$user" "$pass"
expect 代码不应将列表视为纯字符串。使用
提取参数
lassign $argv host user pass
或者如果您的期望太旧而无法 lassign
,请执行
foreach {host user pass} $argv break
或(少干)
set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
我想写一个小的 expect 脚本和另一个 bash 脚本来省去在 ssh 连接中输入密码的工作。
脚本如下:
// ssh.exp, the real workhorse
#!/usr/bin/expect -f
# usage: ./ssh.exp host user pass
set host [lrange $argv 0 0]
set user [lrange $argv 1 1]
set pass [lrange $argv 2 2]
spawn ssh $user@$host
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact
// the bash script to call it
#!/bin/bash
host='my.host.com'
user='someuser'
pass='Qwerty389$'
./ssh.exp $host $user $pass
但是,当测试脚本运行时,ssh服务器总是报错密码。
我尝试转义美元符号,例如 pass='Qwerty389$'
,但无济于事。
将调试语句exp_internal 1
放入expect脚本,显示发送的密码为:
send: sending "{Qwerty389$}\r" to { exp6 } // without escaping $
send: sending "{Qwerty389$}\r" to { exp6 } // escaping $ in password
不确定为什么要将传递给它的密码放在花括号中。我验证了如果密码里没有美元符号,就没有括号。
有什么帮助吗?
shell代码需要引用变量:
./ssh.exp "$host" "$user" "$pass"
expect 代码不应将列表视为纯字符串。使用
提取参数lassign $argv host user pass
或者如果您的期望太旧而无法 lassign
,请执行
foreach {host user pass} $argv break
或(少干)
set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]