简体   繁体   English

如何在Windows子进程中的ssh上运行git fetch

[英]How to run git fetch over ssh in a windows subprocess

I've got some code which needs to grab code from github periodically (on a Windows machine). 我有一些代码需要定期(从Windows计算机上)获取来自github的代码。

When I do pulls manually, I use GitBash, and I've got ssh keys running for the repos I check so everything is fine. 当我手动拉动时,我使用GitBash,并且我为要检查的存储库运行ssh密钥,因此一切都很好。 However when I try to run the same actions in a python subprocess I don't have the ssh services which GitBash provides and I'm unable to authenticate to the repo. 但是,当我尝试在python子进程中运行相同的操作时,我没有GitBash提供的ssh服务,并且无法对存储库进行身份验证。

How should I proceed from here. 我应该从这里继续。 I can think of a couple of different options: 我可以想到几个不同的选择:

  1. I could revert to using https:// fetches. 我可以恢复使用https://提取。 This is problematic because the repos I'm fetching use 2-factor authentication and are going to be running unattended. 这是有问题的,因为我要获取的存储库使用2因子身份验证,并且将在无人值守的情况下运行。 Is there a way to access an https repo that has 2fa from a command line? 有没有办法从命令行访问具有2fa的https存储库?

  2. I've tried calling sh.exe with arguments that will fire off ssh-agent and then issuing my commands so that everything is running more or less the way it does in gitBash, but that doesn't seem to work: 我试过使用将触发ssh-agent的参数调用sh.exe,然后发出命令,以使一切都或多或少地像在gitBash中一样运行,但这似乎不起作用:

     "C:\\Program Files (x86)\\Git\\bin\\sh.exe" -c "C:/Program\\ Files\\ \\(x86\\)/Git/bin/ssh-agent.exe; C:/Program\\ Files\\ \\(x86\\)/Git/bin/ssh.exe -t git@github.com" 

    produces 产生

     SSH_AUTH_SOCK=/tmp/ssh-SiVYsy3660/agent.3660; export SSH_AUTH_SOCK; SSH_AGENT_PID=8292; export SSH_AGENT_PID; echo Agent pid 8292; Could not create directory '/.ssh'. The authenticity of host 'github.com (192.30.252.129)' can't be established. RSA key fingerprint is XXXXXXXXXXX Are you sure you want to continue connecting (yes/no)? yes Failed to add the host to the list of known hosts (/.ssh/known_hosts). Permission denied (publickey). 
  3. Could I use an ssh module in python like paramiko to establish a connection? 我可以像paramiko一样在python中使用ssh模块来建立连接吗? It looks to me like that's only for ssh'ing into a remote terminal. 在我看来,这仅用于ssh'到远程终端。 Is there a way to make it provide an ssh connection that git.exe can use? 有没有办法让它提供git.exe可以使用的ssh连接?

So, I'd be grateful if anybody has done this before or has a better alternative 因此,如果有人以前曾这样做或有更好的选择,我将不胜感激

The git bash set the HOME environment variable, which allows git to find the ssh keys (in %HOME%/.ssh ) git bash设置HOME环境变量,该变量允许git查找ssh键(在%HOME%/.ssh

You need to make sure the python process has or define HOME to the same PATH. 您需要确保python进程具有或将HOME定义为相同的PATH。
As explained in " Python os.environ[“HOME”] works on idle but not in a script ", you need to set HOME to %USERPROFILE% (or, in python, to os.path.expanduser("~") ). 如“ Python os.environ[“HOME”]在空闲但不在脚本中工作 ”中所述,您需要将HOME设置为%USERPROFILE% (或在python中,设置为os.path.expanduser("~") )) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM