Last active
April 13, 2019 16:54
-
-
Save vbarbarosh/86b7d100d0748f9d85f5e4897c65b444 to your computer and use it in GitHub Desktop.
shell_awk_parse_ssh_config – Parse ~/.ssh/config file with awk https://codescreens.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin | |
| # http://redsymbol.net/articles/unofficial-bash-strict-mode/ | |
| set -o nounset -o errexit -o pipefail | |
| # Parse ~/.ssh/config file with awk | |
| # usage: shell_awk_parse_ssh_config | |
| # usage: shell_awk_parse_ssh_config | column -t | |
| cat $HOME/.ssh/config | awk -v RS= -v FS=\\n -v IGNORECASE=1 ' | |
| { | |
| ip = "" | |
| alias = "" | |
| id_file = "" | |
| username = "" | |
| for (j = 1; j <= NF; ++j) { | |
| split($j, tmp, " ") | |
| if (tmp[1] == "Host") { alias = tmp[2] } | |
| if (tmp[1] == "Hostname") { ip = tmp[2] } | |
| if (tmp[1] == "IdentityFile") { id_file = substr(tmp[2], 8) } | |
| if (tmp[1] == "User") { username = tmp[2] } | |
| } | |
| if (ip || alias && alias != "*") { | |
| print ip "\t" alias "\t" username "\t" id_file | |
| } | |
| } | |
| ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment