bash 处理sshpass密码字段中的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26103531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling special characters in password field of sshpass
提问by Manoj
I have a Python script which uses sshpass
for ssh access to a machine
我有一个sshpass
用于 ssh 访问机器的 Python 脚本
Popen(["sshpass","-p", "test!@#", "ssh", "-o UserKnownHostsFile=/dev/null", "-o StrictHostKeyChecking=no", "[email protected]"])
But due to the presence of special characters in password field this command is throwing some error. Is there a way that I can use password with special characters in sshpass or anything else which can be called from Python?
但是由于密码字段中存在特殊字符,此命令会引发一些错误。有没有办法可以在 sshpass 或其他任何可以从 Python 调用的东西中使用带有特殊字符的密码?
The error is: bash: !@#": event not found
错误是: bash: !@#": event not found
回答by Visvendra Singh Rajpoot
Escape character worked in my case. Simply use \
character before '!' or other special characters.
转义字符在我的情况下起作用。只需\
在 '!' 之前使用字符 或其他特殊字符。
回答by usr1234567
Python does not like the special characters. Either escape the symbols by a trailing \
or use a raw string like in r"test!@#"
.
Python 不喜欢特殊字符。要么通过尾随对符号进行转义,要么\
使用像 in 的原始字符串r"test!@#"
。
回答by Aaron Digulla
First of all, using sshpass
with the option -p
means that you publish your password to anyone on the same machine. It's like putting your house key under your rug.
首先,sshpass
与选项一起使用-p
意味着您将密码发布给同一台机器上的任何人。这就像把你的房子钥匙放在你的地毯下面。
The error message comes from the shell which is being used to execute the command above (probably BASH). The character !
is interpreted as special character and means "look in the history for the text after me".
错误消息来自用于执行上述命令的 shell(可能是 BASH)。该字符!
被解释为特殊字符,意思是“在历史中查找我之后的文本”。
The problem is most likely inside of the sshpass
script (since you didn't specify shell=True
in Popen()
). You can try to fix the script by making sure that it uses proper quoting and escaping.
问题很可能出在sshpass
脚本内部(因为您没有shell=True
在 中指定Popen()
)。您可以通过确保它使用正确的引用和转义来尝试修复脚本。
The other solution is to pass env={'SSHPASS': 'test!@#'}
to Popen()
to set the environment for sshpass
as explained in the manpage:
另一种解决方案是通过env={'SSHPASS': 'test!@#'}
对Popen()
以设置环境sshpass
如在手册页解释:
cmd = ['sshpass', '-e', 'ssh', '-o', 'UserKnownHostsFile=/dev/null', ...]
env = os.environ.copy()
env['SSHPASS'] = 'test!@#'
Popen(cmd, env=end)
Note: You should split the "-o key=value"
into "-o", "key=value"
.
注意:您要拆分"-o key=value"
成"-o", "key=value"
。
Related:
有关的: