UCProgrammer
UCProgrammer

Reputation: 557

subprocess.communicate() won't supply input to Popen calling ssh via git

I am using python 2.7 so I am unable to use what I think would work (subprocess.check_ouput() with input argument) and I am trying to pass the string 'yes' to the Popen object. Here is my code.

def clone(ssh_url):
    sub_list = ['git', 'clone', ssh_url]
    cmd = subprocess.Popen(sub_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                           universal_newlines=True)
    output, err = cmd.communicate(input='yes')
        print output
        if err is not None:
            print err

ssh_url = '[email protected]:User1/test-work.git'
clone(ssh_url)

So I am trying to clone a gitlab project. I haven't used the ssh key on this machine before so I am receiving a message saying what the value of the ECDSA key fingerprint is and whether or not I want to continue connecting (yes/no). I want to pass 'yes' to this selection when it is presented, however, the input I supplied communicate() is not received and I get a Host Key verification failed. fatal: Could not read from remote repository error from git.

Is anyone aware of a way to make this work? Is communicate() blocking? I tried using the threading example here, Use subprocess.communicate() to pipe stdin without waiting for process, in the accepted response but that did not work either.

Upvotes: 2

Views: 1184

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295639

This is intentional, desired behavior from SSH. It's not anything you're doing wrong in your Python -- rather, SSH intentionally doesn't use stdin to collect answers to security-related prompts (which ensures that its stdin is passed through to the remote command, rather than consumed by SSH itself).

git_env = os.environ.copy()
git_env['GIT_SSH_COMMAND'] = 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
cmd = subprocess.Popen(sub_list,
  stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
  universal_newlines=True, env=git_env)

Upvotes: 2

Related Questions