我尝试从Python发送一些SSH命令。它工作得很好,但是一旦嵌套引号,我就会陷入混乱。ssh与Python:如何正确使用转义和引号
所以在很基本情况我只是做:现在
cmd = "ssh -A -t [email protected] \"do_on_host\"")) # notice the escaped quotes
Popen(cmd, ..., shell=True)
,在一个更复杂的情况,我想使用用户指定的命令。请注意,我逃过周围的用户命令和用户命令引号:
user_cmd = "prog -arg \"a string arg\""
cmd = "ssh -A -t [email protected] \"do_on_host; " + user_cmd + "\"")) # notice the escaped quotes
Popen(cmd, ..., shell=True)
它,当我尝试的SSH两个跳变得更糟。请注意,我想更多的东西有单,双引号来转义:
user_cmd = "prog -arg \"a string arg\""
cmd = "ssh -A -t [email protected] \"ssh -A -t [email protected] '" + user_cmd + "' \""
Popen(cmd, ..., shell=True)
它可能变得更糟:该片段可能在脚本do_over_ssh.py
其中user_cmd
可能与从控制台读取使用,如:
$ ./do_over_ssh.py my_cmd -arg "a string arg"
毕竟,我完全困惑。
在Python 3.5中处理嵌套引号的规范和干净的方法是什么?
(重要提示:其他的答案是Python 2.7版!)
为什么不使用Fabric3来运行远程用户命令?多跳也工作,如在这里提到的https://stackoverflow.com/questions/20658874/how-to-do-multihop-ssh-with-fabric –