Unable to SSH via Python subprocess

情到浓时终转凉″ 提交于 2020-02-16 08:11:33

问题


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

ssh -i  <pemfile location> -A -o 'proxycommand ssh -i <pemfile location> ec2-user@<bastion ip address> -W %h:%p' hadoop@<machine ip>

This command is rather very long. So I tried to write a python script which takes ip addresses and pemfile location as inputs and does ssh.

#!/usr/local/bin/python3
import argparse
import subprocess
import os
import sys
import errno

parser = argparse.ArgumentParser(description="Tool to ssh into EMR via a bastion host")
parser.add_argument('master', type=str, help='IP Address of the EMR master-node')
parser.add_argument('bastion', type=str, help='IP Address of bastion EC2 instance')
parser.add_argument('pemfile', type=str, help='Path to the pemfile')

args = parser.parse_args()

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

command = ""

for w in cmd_list:
    command = command + " " + w

print("")
print("Executing command : ", command)
print("")

subprocess.call(cmd_list)

I get the following error when I run this script :

command-line: line 0: Bad configuration option: 'proxycommand

But I am able to run the exact command via bash.

Why is the ssh from python script failing then?


回答1:


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.



来源:https://stackoverflow.com/questions/59999659/unable-to-ssh-via-python-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!