简体   繁体   中英

Specifying multiple commands for GIT_SSH_COMMAND

Background

Most of the systems I use at work are only available on the corporate LAN, or a VPN. While connected to those networks, my connections go through a proxy, which is protected with HTTP Basic Authorization.

However, one of my projects requires me to connect to GitHub with SSH keys. This is all set up and working fine when outside of the corporate network/VPN (ie, without a proxy), but I've been trying to get it working while inside that network too, as I don't want to have to keep disconnecting and reconnecting the VPN to access the other protected systems mentioned earlier. And I've gotten pretty close.

Many articles and SO answers encourage adding an entry to my .ssh/config for github.com, and using the ProxyCommand option with things like 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 .

I can successfully connect to my GitHub repo behind the VPN using the following ssh config:

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

And then running the following commands:

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

Question

Now, I want some way of running these commands whenever I clone with SSH. I don't want an alias for git , because then desproxy will start every time I run a git command - it should only start when running git clone|fetch|push|etc on a repo with an SSH-based remote. As I understand it, that's the purpose of the GIT_SSH_COMMAND environment variable, or the core.sshCommand git configuration variable.

Where I'm getting stuck - I have multiple commands that need to be run before ssh , so I'd expect my configuration to look like one of the below:

# 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

But both of these methods fail

# 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

So, what is the correct way to use multiple commands for GIT_SSH_COMMAND ?


Update

I tried @phd's suggestion of omitting the semicolon after the ampersand in my GIT_SSH_COMMAND ; ie, using the below:

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

However, I get a new error now:

❯ 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

Syntax error: ";" unexpected

The error came from shell. Omit the semicolon completely, & is a command separator by itself:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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