Python script to git clone without entering a password at the prompt

那年仲夏 提交于 2021-02-10 15:52:33

问题


I am trying to clone a project from the private git repository git clone gitolite@10.10.10.55:/Intel/BareRepos/lteue.git using the Python script. The problem with my script is I need to enter the password manually every time for cloning the project from local repository.

Is there any pythonic way to clone the project without entering any password manually?

This is the script which I had written.

import os

path = path/to/save/cloned/project

os.chdir(path)

os.system("git clone gitolite@10.10.10.55:/Intel/BareRepos/lteue.git")#...Clonning

回答1:


The best thing to do it use public/private keys. However, I don't know gitolite at all. (You may want to add that into the tags.)

Note, I would not recommend doing the following, unless you know no one unauthorized will see your script. This is bad security practice, etc.

If you really want this to be in Python, I would use subprocess.Popen.

from subprocess import Popen, PIPE

password = 'rather_secret_string'

proc = Popen(['git', 'clone', 'gitolite@10.10.10.55:/Intel/BareRepos/lteue.git'], stdin=PIPE)

proc.communicate(password)



回答2:


I would use subprocess to do this.

So you use Popen() to create a process and then you can communicate with it. You do need to use PIPE to get your password to the input.

from subprocess import Popen, PIPE
process = Popen(["git", "clone", "gitolite@10.10.10.55:/Intel/BareRepos/lteue.git"], stdin=PIPE)

process.communicate('password that I send')

Something like this will probably work.

You can also use Pexpect but I am not familiar with that library.




回答3:


I don't know why the above answers didn't work for me. But I come up with the new solution which will work for sure and it is very simple.

Here is my complete code:

import os
import sys
import shutil

path        =   "/path/to/store/your/cloned/project" 
clone       =   "git clone gitolite@10.10.10.55:/Intel/BareRepos/lteue.git" 

os.system("sshpass -p your_password ssh user_name@your_localhost")
os.chdir(path) # Specifying the path where the cloned project has to be copied
os.system(clone) # Cloning

print "\n CLONED SUCCESSFULLY.! \n"


来源:https://stackoverflow.com/questions/45219420/python-script-to-git-clone-without-entering-a-password-at-the-prompt

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