python—自动向交互式shell提供git密码的方法

pu3pd22g  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(373)

为了让下面的代码在收到请求密码的响应时以交互方式提供密码,需要更改哪些特定语法?
要求:
Python3程序必须自动化将私有远程github存储库克隆到本地计算示例的过程。
密码不得泄漏到日志中。因此,我们不能使用: git clone https://username:password@github.com/username/repository-name.git 必须同时在linux和windows上运行。
当前代码:
为此,我们目前正在试验的python3代码是:

def runShellCommandInWorkingDir(commandToRun, workingDir):  
  proc = subprocess.Popen( commandToRun,cwd=workingDir, stdout=subprocess.PIPE, shell=True)  
  while True:  
    line = proc.stdout.readline()  
    if line:  
      thetext=line.decode('utf-8').rstrip('\r|\n')  
      decodedline=ansi_escape.sub('', thetext)  
      print(decodedline)  
    else:  
      break  

repoUrlCred = "https://" + username + "@github.com/" + username + "/repository-name.git"  
gitCloneCommand = "git clone " + repoUrlCred  
runShellCommandInWorkingDir(gitCloneCommand, "D:\\path\\to\\working\\Directory\\")

目前的结果:
当我们运行如上所示的当前代码时,python 3程序中途停止程序,并要求用户手动输入密码,如下所示:

Cloning into 'repository-name'...  
Logon failed, use ctrl+c to cancel basic credential prompt.  
Password for 'https://username@github.com':

为了让Python3程序在运行时自动输入密码而不停下来向用户询问密码,上面必须更改哪些特定的Python3语法?

tp5buhyn

tp5buhyn1#

建议:
生成专用于此客户端的ssh密钥
在github中为用户注册公钥 username 使用ssh克隆存储库

相关问题