This is my first post here, so please bear with me…
I have discovered the amazing powers of .ssh/config after having fully configured my .bash_aliases file, and as I’m pretty lame with programming, I was looking for a nice way to parse the connection details from the bash_aliases file into the ~/.ssh/config file.
So, going into details, my bash_aliases looks like this:
alias serverA='ssh -i ~/.ssh/serverA.key -o $SRVR_ALV user@serverAhostname'
alias serverB='ssh -i ~/.ssh/serverB.key -o $SRVR_ALV user@serverBhostname'
....
Where I have defined a variable for ServerAliveInterval in $SRVR_ALV.
My intention is to parse that entry into this and in the meantime get rid of that ugly variable
Host serverA
HostName serverAhostname
IdentityFile ~/.ssh/serverA.key
Host serverB
.....
What do you think is the best way to perform this? I was looking for a nice bash script, or perhaps using vi capabilities.
Thanks in advance!
How about some Perl?
If you like what you see, redirect it to append to
>> ~/.ssh/config[Update] – an explanation for the non-Perl user as to what’s going on:
perl -ne:-emeans, ‘The next argument is the script to (e)xecute’.-n
means, '*run the given script once per line*' - so-necan sort-ofsed`.emulate
The perl code:
reads: for only the lines that match the regexp
/^alias ../, print a string (qq{...}is that same as"..."in Perl), and the interpolated variables"\$1","\$4"etc are the result of what was captured in the regexp.perl -neexpects input, which comes from< ~/.bash_aliases.