gpt4 book ai didi

linux - 在期望脚本中转义连字符和引号

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:23 26 4
gpt4 key购买 nike

您好,我正在使用 expect 来自动执行登录任务。但如果密码以连字符开头,脚本将失败我怎么能逃脱呢

我在正确转义“”或其他字符时遇到了很多麻烦有没有一种方法可以对我的所有字符进行编码并发送期望编码的字符串在发送之前对其进行解码

这是我的脚本

#!/usr/bin/expect -f
spawn ssh -o "PubkeyAuthentication no" -l user 10.10.10.10
expect "password: "
send "-cpass\'ok\r"
expect "$ "

最佳答案

来自手册页:

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"

注意:尝试传递任何以连字符开头的字符串时会遇到问题;这将不起作用:发送“-cpass”,如果您的字符串有连字符,您需要使用 -- 标志。

关于linux - 在期望脚本中转义连字符和引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42393925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com