Bob Risky
Bob Risky

Reputation: 935

Is it possible to write a 'no-op' Proxycommand for SSH?

I recently edited my SSH config so that the Proxycommand links to an executable bash file, where I want to perform some logic. If condition1 is met, then I want to perform a certain Proxycommand. Otherwise, I want a "no-op"; essentially I don't want to proxy in this case.

My SSH config looks like:

Host *
    ProxyCommand <link_to_bashfile> %h %p

However, if I just don't do anything in the bash file, I get the following error:

ssh_exchange_identification: Connection closed by remote host

Is there a way to essentially emulate ProxyCommand none when condition1 is not met in the bash file? Thanks!

Upvotes: 4

Views: 1414

Answers (2)

Bob Risky
Bob Risky

Reputation: 935

Turns out the following code works:

host=$1
port=$2

if <condition>; then
    whatever the actual command is to connect
else
    exec nc $1 $2
fi

Upvotes: 2

chepner
chepner

Reputation: 532368

You might be able to use the -o option to override the ProxyCommand directive found in the config file, which lets you just run ssh without getting into an endless loop of proxy commands. That is, in your bash file,

host=$1
port=$2

if <condition>; then
    whatever the actual command is to connect
else
    ssh -o ProxyCommand=none -p "$port" "$host"
fi

Upvotes: 3

Related Questions