Home > Software engineering >  Specifying multiple commands for GIT_SSH_COMMAND
Specifying multiple commands for GIT_SSH_COMMAND

Time:12-01

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 (i.e., 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 [email protected]:<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 [email protected]:<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 [email protected]:<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; i.e., 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 [email protected]:<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

Update 2

I've accepted @phd's answer, as it does answer this question, even it doesn't solve my specific issue. I worked on a helper script to get this working, which I will post about later.

CodePudding user response:

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"
  • Related