在期望脚本中转义连字符和引号
escaping hyphen and quotes in expect script
您好,我正在使用 expect 自动执行登录任务。
但如果密码以连字符开头,脚本将失败
我怎样才能逃脱
我在正确转义“”或其他字符时遇到了很多麻烦有没有一种方法可以对我的所有字符进行编码并发送期望编码的字符串在发送之前对其进行解码
这是我的脚本
#!/usr/bin/expect -f
spawn ssh -o "PubkeyAuthentication no" -l user 10.10.10.10
expect "password: "
send "-cpass\'ok\r"
expect "$ "
您可以尝试将密码保存在变量中:
password=-cpass\'ok\r
send "$password"
您只需在每个 '
、"
或空格前写一个 \
。
来自手册页:
The -- flag forces the next argument to be interpreted as a string rather than a flag. Any string can be preceded by "--" whether
or not it actually looks
like a flag. This provides a reliable mechanism to specify variable strings without being tripped up by those that accidentally
look like flags. (All
strings starting with "-" are reserved for future options.)
这意味着您应该 send
像这样写:
send -- "-cpass\'ok\r"
注意:尝试传递任何以连字符开头的字符串时会遇到问题;这将不起作用:send "-cpass"
,如果您的字符串有一个连字符,您需要使用 --
标志。
您好,我正在使用 expect 自动执行登录任务。 但如果密码以连字符开头,脚本将失败 我怎样才能逃脱
我在正确转义“”或其他字符时遇到了很多麻烦有没有一种方法可以对我的所有字符进行编码并发送期望编码的字符串在发送之前对其进行解码
这是我的脚本
#!/usr/bin/expect -f
spawn ssh -o "PubkeyAuthentication no" -l user 10.10.10.10
expect "password: "
send "-cpass\'ok\r"
expect "$ "
您可以尝试将密码保存在变量中:
password=-cpass\'ok\r
send "$password"
您只需在每个 '
、"
或空格前写一个 \
。
来自手册页:
The -- flag forces the next argument to be interpreted as a string rather than a flag. Any string can be preceded by "--" whether or not it actually looks like a flag. This provides a reliable mechanism to specify variable strings without being tripped up by those that accidentally look like flags. (All strings starting with "-" are reserved for future options.)
这意味着您应该 send
像这样写:
send -- "-cpass\'ok\r"
注意:尝试传递任何以连字符开头的字符串时会遇到问题;这将不起作用:send "-cpass"
,如果您的字符串有一个连字符,您需要使用 --
标志。