3

Here's my sample python code

def gitClone():
    proc = subprocess.Popen(['git', 'clone', 'https://someuser@sailclientqa.scm.azurewebsites.net:443/sailclientqa.git'], stdout=subprocess.PIPE)
    stdout,stderr = proc.communicate('mypassword')
    print(stddata)
    print (stderr) 

Every execution results in prompt for password. What's the best way for me to do this git clone from python without password prompt.

I'd appreciate your expertise/advice

Krishnan Sriram
  • 5,037
  • 5
  • 21
  • 31
  • I tried that. Here's my code `proc = subprocess.Popen(['git', 'clone', 'https://spaclientdistqa@sailclientqa.scm.azurewebsites.net:443/sailclientqa.git'], stdin=subprocess.PIPE) proc.communicate('5pac1ientd15tqa')` Still did not work – Krishnan Sriram Feb 07 '17 at 20:35
  • I can't setup SSH for this case. This is a temp GIT repository on Azure for deployment. I can't get https://user:pass/someurl/.git work either. Only solution I have is to figure it out through python. Not sure how this is a duplicate, when the original question is about git where as I want this addressed by python. I do see the similarity from your point, though miney is more specific from python perspective – Krishnan Sriram Feb 07 '17 at 20:51

1 Answers1

1

The naive approach would be to add stdin=subprocess.PIPE to your command so you can feed it password input. But that's not that simple with password inputs which use special ways of getting keyboard input.

On the other hand, as stated in this answer it is possible to pass the password in command line (although not advised since password is stored in git command history with the command line !).

I would modify your code as follows:

  • add a password parameter. If not set, use python getpass module to prompt for password (maybe you don't need that)
  • if the password is properly set, pass it in the modified command line

my proposal:

import getpass

def gitClone(password=None):
    if password is None:
        password = getpass.getpass()
    proc = subprocess.Popen(['git', 'clone', 'https://someuser:{}@sailclientqa.scm.azurewebsites.net:443/sailclientqa.git'.format(password)], stdout=subprocess.PIPE)
    stdout,stderr = proc.communicate()
    print(stddata)
    print (stderr)

An alternative if the above solution doesn't work and if you cannot setup ssh keys, is to use pexpect module which handles special password input streams:

def gitClone(mypassword):
    child = pexpect.spawn('git clone https://someuser@sailclientqa.scm.azurewebsites.net:443/sailclientqa.gi')
    child.expect ('Password:')
    child.sendline (mypassword)

It has the nice advantage of not storing the password in git command history.

source: How to redirect data to a "getpass" like password input?

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219