且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法通过 Python 子进程 SSH

更新时间:2023-12-05 13:43:16

您犯了(常见的)错误,将语法引号与字面引号混合使用.在命令行中,shell 在将字符串传递给您正在运行的命令之前删除所有引号;你应该做同样的事情.

You are making the (common) mistake of mixing syntactic quotes with literal quotes. At the command line, the shell removes any quotes before passing the string to the command you are running; you should simply do the same.

cmd_list = ["ssh", "-i", args.pemfile, "-A",
    "-o", "proxycommand ssh -i {} ec2-user@{} -W %h:%p".format(
        args.pemfile, args.bastion), "hadoop@{}".format(args.master)]

另请参阅何时将引号括在 shell 变量周围? 讨论引用如何在 shell 中工作,也许 实际含义子进程中的 'shell=True' 作为 Python 端的起点.

See also When to wrap quotes around a shell variable? for a discussion of how quoting works in the shell, and perhaps Actual meaning of 'shell=True' in subprocess as a starting point for the Python side.

然而,编写交互式 SSH 会话的脚本会很脆弱;对于这类事情,我建议您查看合适的 Python 库,例如 Paramiko.

However, scripting interactive SSH sessions is going to be brittle; I recommend you look into a proper Python library like Paramiko for this sort of thing.