[英]Migrating from .bash_aliases to ~/.ssh/config
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. 在完全配置完.bash_aliases文件之后,我发现了.ssh / config的强大功能。由于我对编程很la脚,我一直在寻找一种将bash_aliases文件的连接详细信息解析为〜/的好方法。 .ssh / config文件。
So, going into details, my bash_aliases looks like this: 因此,进入细节,我的bash_aliases如下所示:
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. 我在$ SRVR_ALV中为ServerAliveInterval定义了一个变量。
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.
我一直在寻找一个不错的bash脚本,或者使用vi功能。 Thanks in advance!
提前致谢!
How about some Perl? Perl呢?
perl -ne "/^alias (.*)='ssh -i (.*) -o (.*) (.*)\@(.*)'/ and
print qq{Host \$1\nHostName \$5\nIdentityFile \$2\n\n}" < \
~/.bash_aliases
If you like what you see, redirect it to append to >> ~/.ssh/config
如果您喜欢所看到的内容,请将其重定向以附加到
>> ~/.ssh/config
[Update] - an explanation for the non-Perl user as to what's going on: [更新] -针对非Perl用户的说明:
perl -ne
: -e
means, 'The next argument is the script to ( e )xecute' . perl -ne
: -e
表示, “下一个参数是( e )xecute的脚本” 。 -n means, '*run the given script once per line*' - so
-ne can sort-of emulate
sed`. -n
means, '*run the given script once per line*' - so
ne can sort-of emulate
sed can sort-of emulate
。
The perl code: Perl代码:
/^alias (.*)='ssh -i (.*) -o (.*) (.*)\@(.*)'/ and
print qq{Host \$1\nHostName \$5\nIdentityFile \$2\n\n}
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. 读取:仅对与regexp
/^alias ../
匹配的行,打印字符串( qq{...}
与Perl中的"..."
相同),以及插值变量"\\$1"
, "\\$4"
等是在正则表达式中捕获的结果。
perl -ne
expects input , which comes from < ~/.bash_aliases
. perl -ne
需要输入 ,它来自< ~/.bash_aliases
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.