2

这是我在这里的第一篇文章,所以请多多包涵……

在完全配置了我的 .bash_aliases 文件后,我发现了 .ssh/config 的惊人功能,并且由于我对编程很蹩脚,我正在寻找一种很好的方法来将 bash_aliases 文件中的连接详细信息解析到 ~/ .ssh/配置文件。

所以,深入细节,我的 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'
....

我在 $SRVR_ALV 中为 ServerAliveInterval 定义了一个变量。

我的意图是解析这个条目,同时摆脱那个丑陋的变量

Host serverA
HostName serverAhostname
IdentityFile ~/.ssh/serverA.key

Host serverB
.....

您认为执行此操作的最佳方法是什么?我正在寻找一个不错的 bash 脚本,或者可能使用 vi 功能。提前致谢!

4

1 回答 1

0

一些 Perl 怎么样?

perl -ne "/^alias (.*)='ssh -i (.*) -o (.*) (.*)\@(.*)'/ and 
    print qq{Host \$1\nHostName \$5\nIdentityFile \$2\n\n}" < \
 ~/.bash_aliases

如果您喜欢所看到的,请将其重定向到附加到>> ~/.ssh/config

[更新] - 对非 Perl 用户的解释:

perl -ne:-e表示,'下一个参数是 ( e ) 执行的脚本'。-n means, '*run the given script once per line*' - so-ne can sort-of emulatesed`。

perl 代码:

/^alias (.*)='ssh -i (.*) -o (.*) (.*)\@(.*)'/ and 
print qq{Host \$1\nHostName \$5\nIdentityFile \$2\n\n}

读取:仅对于与 regexp 匹配的行/^alias ../,打印一个字符串(与 Perlqq{...}中的相同"..."),并且插值变量"\$1""\$4"是在 regexp 中捕获的结果。

perl -ne期望输入,它来自< ~/.bash_aliases.

于 2012-12-01T00:06:55.637 回答