在 Python 中正确使用原始字符串通过 SSH 发送命令
Using raw string properly to send commands over SSH in Python
我有一个很长的命令来使用 Paramiko 通过 SSH 发送,我需要使用 "r" 参数来包装字符串,但是 IDE 仍然告诉我它写得不好。
字符串是这样的:
somecommand get -n somestuff sa/management --template='{{range .secrets}}{{printf "%s\n" .name}}{{end}}'
我试过:
command = r'somecommand get -n somestuff sa/management --template='{{range .secrets}}{{printf "%s\n" .name}}{{end}}'
但是出错了。
这可能是一件非常容易做到的事情....
首先,您在字符串末尾缺少引号。
第二个问题是你不能在这样的字符串中使用引号。
r'some'thing'
在
期间将不起作用
r'some"thing'
会起作用的。
由于字符串中的引号中有引号,因此请改用三引号字符串:
r"""some"thi'ng"""
所以你的字符串的工作版本是:
command = r"""somecommand get -n somestuff sa/management --template='{{range .secrets}}{{printf "%s\n" .name}}{{end}}'"""
我有一个很长的命令来使用 Paramiko 通过 SSH 发送,我需要使用 "r" 参数来包装字符串,但是 IDE 仍然告诉我它写得不好。 字符串是这样的:
somecommand get -n somestuff sa/management --template='{{range .secrets}}{{printf "%s\n" .name}}{{end}}'
我试过:
command = r'somecommand get -n somestuff sa/management --template='{{range .secrets}}{{printf "%s\n" .name}}{{end}}'
但是出错了。 这可能是一件非常容易做到的事情....
首先,您在字符串末尾缺少引号。 第二个问题是你不能在这样的字符串中使用引号。
r'some'thing'
在
期间将不起作用r'some"thing'
会起作用的。 由于字符串中的引号中有引号,因此请改用三引号字符串:
r"""some"thi'ng"""
所以你的字符串的工作版本是:
command = r"""somecommand get -n somestuff sa/management --template='{{range .secrets}}{{printf "%s\n" .name}}{{end}}'"""