{
  "path": "/posts/2015/2015-09-23-bash-ssh-host-management",
  "site": "at://did:plc:mracrip6qu3vw46nbewg44sm/site.standard.publication/self",
  "tags": [
    "code",
    "ssh",
    "awk"
  ],
  "$type": "site.standard.document",
  "title": "Bash SSH host management",
  "updatedAt": "2015-09-23T18:03:00.000Z",
  "publishedAt": "2015-09-23T18:03:00.000Z",
  "textContent": "If you have a lot of servers to which you frequently connect, keeping track of IP addresses, pem files, and credentials can be tedious. SSH config files are great for this problem, but they don't play well with bash. I wanted to store all of my hosts' info in a config file but still have access to the HostNames since sometimes I just need the IP address of a server to use elsewhere.\n\nAn example ssh config file might look like this:\n\n~/.ssh/config\n\nWe can use AWK to parse the ~/.ssh/config file and create bash variables with the name of the Hosts and the values of the HostName respectively.\n\nFirst, cat ~/.ssh/config creates a stream of our config file. Next, awk '$1 ~ /Host/ { print $2 }' finds all lines that start with \"Host\" and prints the value after them seperated (by default) by any type of whitespace. The last bit does most of the work:\n\nThe output of the command is:\n\nfor each of your Hosts in your config file. _NOTE_: I recommend only using this trick for server names with exclusively alphanumeric characters. Otherwise, you may see strange errors like -bash: <server_name>=<host_name>: command not found when the shell initializes.\n\nYou can add this to your bash initialization to get access to all your HostNames as variables from your ssh config.\n\n.bashrc\n\nCheers!",
  "canonicalUrl": "https://www.danielcorin.com/posts/2015/2015-09-23-bash-ssh-host-management"
}