为 GIT_SSH_COMMAND 指定多个命令

Specifying multiple commands for GIT_SSH_COMMAND

背景

我在工作中使用的大多数系统只能在公司 LAN 或 VPN 上使用。连接到这些网络时,我的连接通过代理进行,该代理受 HTTP 基本授权保护。

但是,我的一个项目要求我使用 SSH 密钥连接到 GitHub。在公司 network/VPN 之外(即没有代理),这一切都已设置好并且工作正常,但我也一直试图让它在该网络内工作,因为我不想拥有继续断开并重新连接 VPN 以访问前面提到的其他受保护系统。我已经非常接近了。

许多文章和 SO 答案鼓励为 github.com 添加一个条目到我的 .ssh/config,并使用 ProxyCommand 选项和 nc or corkscrew. But none of these seemed to play well with my proxy's requirement for authentication. The one tool that has worked is desproxy.[=31= 之类的东西]

我可以使用以下 ssh 配置成功连接到我的 GitHub VPN 存储库:

Host github.com
    IdentityFile <key>
    HostName 127.0.0.1
    Port 2222

然后运行执行以下命令:

set PROXY_USER=$proxy_user:$proxy_pw
desproxy ssh.github.com 443 $proxy_host $proxy_port 2222 &
git clone git@github.com:<user>/<repo>.git

问题

现在,每当我使用 SSH 克隆时,我都希望通过某种方式 运行 执行这些命令。我不想要 git 的别名,因为这样 desproxy 将在每次我 运行 一个 git 命令时启动 - 它应该只在 运行ning 时启动git clone|fetch|push|etc 在基于 SSH 远程的仓库上。据我了解,这就是 GIT_SSH_COMMAND 环境变量或 core.sshCommand git 配置变量的目的。

我卡住的地方 - 我有多个命令需要在 ssh 之前 运行,所以我希望我的配置看起来像以下之一:

# environment variable
export GIT_SSH_COMMAND="set PROXY_USER=$proxy_user:$proxy_pw; desproxy ssh.github.com 443 $proxy_host $proxy_port 2222 &; ssh"

# git configuration variable (using '\;' instead of ';' as that is used for comments in git configuration file)
[core]
    sshCommand = set PROXY_USER=$proxy_user:$proxy_pw\; desproxy ssh.github.com 443 $proxy_host $proxy_port 2222 &\; ssh

但是这两种方法都失败了

# environment variable
❯ git clone git@github.com:<user>/<repo>.git
Cloning into '<repo>'...
set PROXY_USER=$proxy_user:$proxy_pw; desproxy ssh.github.com 443 $proxy_host $proxy_port 2222 &; ssh: 1: Syntax error: ";" unexpected
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

# git configuration variable 
❯ git clone git@github.com:<user>/<repo>.git
fatal: bad config line 6 in file /home/murchu27/.gitconfig

那么,GIT_SSH_COMMAND使用多个命令的正确方法是什么?


更新

我尝试了@phd 的建议,在我的 GIT_SSH_COMMAND 中省略了 & 符号后的分号;即,使用以下内容:

export GIT_SSH_COMMAND="set PROXY_USER=$proxy_user:$proxy_pw; desproxy ssh.github.com 443 $proxy_host $proxy_port 2222 & ssh"

但是,我现在收到一个新错误:

❯ git clone git@github.com:<user>/<repo>.git
Cloning into '<repo>'...
bind: Address already in use
fatal: protocol error: bad line length character:
---
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: Could not resolve hostname PROXY_USER=<proxy_user>:<proxy_pw>: Name or service not known

更新 2

我接受了@phd 的回答,因为它确实回答了这个问题,即使它没有解决我的具体问题。我编写了一个帮助脚本来使它正常工作,稍后我将 post。

Syntax error: ";" unexpected

错误来自 shell。完全省略分号,& 本身就是命令分隔符:

export GIT_SSH_COMMAND="set PROXY_USER=$proxy_user:$proxy_pw; desproxy ssh.github.com 443 $proxy_host $proxy_port 2222 & ssh"