Unable to SSH via Python subprocess

后端 未结 1 1724
走了就别回头了
走了就别回头了 2021-01-26 03:09

I need to ssh into a machine via a bastion. Therefore the command is rather very long for this:

ssh -i   -A -o \'proxycommand ssh -i <         


        
相关标签:
1条回答
  • 2021-01-26 03:46

    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)]
    

    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.

    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.

    0 讨论(0)
提交回复
热议问题