【问题标题】:How to run git fetch over ssh in a windows subprocess如何在 Windows 子进程中通过 ssh 运行 git fetch
【发布时间】:2014-11-27 02:53:00
【问题描述】:

我有一些代码需要定期从 github 获取代码(在 Windows 机器上)。

当我手动拉取时,我使用 GitBash,并且我已经为我检查的存储库运行了 ssh 密钥,所以一切都很好。但是,当我尝试在 python 子进程中运行相同的操作时,我没有 GitBash 提供的 ssh 服务,并且我无法对 repo 进行身份验证。

我应该如何从这里开始。我可以想到几个不同的选择:

  1. 我可以恢复使用 https:// 获取。这是有问题的,因为我正在获取的存储库使用 2 因素身份验证并且将在无人值守的情况下运行。有没有办法从命令行访问具有 2fa 的 https 存储库?

  2. 我尝试使用将触发 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"
    

    生产

    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. 我可以在 python 中使用像 paramiko 这样的 ssh 模块来建立连接吗?在我看来,这只是为了 ssh'ing 到远程终端。有没有办法让它提供 git.exe 可以使用的 ssh 连接?

所以,如果有人以前这样做过或有更好的选择,我将不胜感激

【问题讨论】:

    标签: python windows git github ssh


    【解决方案1】:

    git bash 设置了HOME 环境变量,它允许git 找到ssh 密钥(在%HOME%/.ssh

    您需要确保 python 进程具有或定义 HOME 到相同的 PATH。
    如“Python os.environ[“HOME”] works on idle but not in a script”中所述,您需要将HOME 设置为%USERPROFILE%(或在python 中设置为os.path.expanduser("~"))。

    【讨论】:

    • 成功了,谢谢!正确设置 HOME 变量使其运行
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2012-04-21
    相关资源
    最近更新 更多