Score:0

Running a shell script with ProxyCommand that computes an IP and starts an SSH session

ng flag

I would like my SSH config to call out to a script in certain situations to resolve a different IP address than what I provide. In this situation, I don't actually want to proxy through another host, simply change the host that's being SSH'd to (although in some situations I would like to proxy through a host, hence bundling this into one script).

This is a rather long script and isn't really designed to be a oneliner in the ProxyCommand directive.

This is an extract from the script that shows the issue:

#!/bin/bash

instance_id="$1"
user="$2"
port="$3"

if [[ $instance_id == *"i"* ]]; then
  filter="--instance-ids ${instance_id}"
else
  filter="--filters Name=ip-address,Value=${instance_id}"
fi

public_ip=$(aws ec2 describe-instances $filter | jq -r '.Reservations[0].Instances[0].PublicIpAddress')

if [[ $public_ip == "null" ]]; then
  true
else
  ssh $user@$public_ip -p $port
fi

And this is my ~/.ssh/config:

Host i-* mi-* 172.2* 172.3*
   User cbell
   ProxyCommand ~/.ssh/aws_iea_proxy_command.sh %h %r %p

If I try to ssh to an instance ID, eg ssh i-030ee3dd4f2712b3b, the script is triggered, but I'm not given an interactive shell.

[connor@Enigma:~/.ssh] 130 $ ssh i-030ee3dd4f2712b3b
Pseudo-terminal will not be allocated because stdin is not a terminal.
-bash: line 1: SSH-2.0-OpenSSH_8.2p1: command not found

How do I get the SSH command running in a script via ProxyCommand to work?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.