Reputation: 5872
I am wanting to modify my ~/.ssh/config
file so that it reads IP address from a text file. For example:
Host prod-app
HostName ip-address-here
User root
Port 22
IdentityFile ~/.ssh/my-key
Since my EC2 instances have different public IP addresses after deployments, I am trying to figure out a way that I can just simply have this read from a text file. So rather than above, it would look something like this:
Host prod-app
HostName $(cat $HOME/Documents/app_prod.txt)
User root
Port 22
IdentityFile ~/.ssh/my-key
That doesn't work because of the following error:
/Users/nutella/.ssh/config line 33: garbage at end of line; "$HOME/Documents/app_prod.txt)"
Is there an easy way to get this work or a better way by chance?
Upvotes: -1
Views: 332
Reputation: 3623
The Include
option is very useful in combination with the Match Exec
feature.
If you have a script host_config
which generates "ssh_config" commands into a file such as ~/.ssh/session-setup
, you can then "include ~/.ssh/session-setup" and have those incorporated into your session.
eg
Match Host prod-app Exec "host_config"
Include ~/.ssh/session-setup
The host_config
script could do something like
#!/bin/bash
source ~/.app/.env
cat <<EOF > ~/.ssh/session-setup
Hostname ${APP_HOST}
User ${APP_USER}
SetEnv APP_TOKEN=${APP_TOKEN}
EOF
Once connected to the correct host, the "APP_TOKEN" environment could be set, if allowed by the sshd_config
.
Note the the default sshd_config
accepts all environment variables starting with LC_
, so you could be tricky and name your variables as LC_APP_TOKEN
, then reassign them to the correct names in the ~/.bashrc
...
Upvotes: 0
Reputation: 10314
You could use an include statement:
Host prod-app
include ~/Documents/app_prod.txt
User root
Port 22
IdentityFile ~/.ssh/my-key
app_prod.txt:
HostName 192.0.2.1
Upvotes: 3