在 KSH 中循环 SSH 的更有效方法?
我目前在脚本中有以下几行代码:
set -A ARRAY OPTION1 OPTION2 OPTION3 OPTION4
set -A matches
for OPTION in ${ARRAY[@]}; do
DIFF=$(ssh $USER@$host " diff $PERSONALCONF $PRESETS$OPTION" )
if [[ $DIFF == "" ]]; then
set -A matches"${matches[@]}" $OPTION
fi
done
基本上,我有一个循环,它遍历预定义数组中的每个元素,连接到远程服务器(每次都是同一服务器),然后将文件与文件进行比较由使用 diff 命令的循环定义。基本上,它将personal.conf 文件与personal.conf.option1、personal.conf.option2 等进行比较。如果没有差异,则将其添加到数组中。如果有差异,则不会发生任何事情。
我想知道是否可以通过仅通过 SSH 连接一次来执行此操作或获得相同的结果(将匹配文件存储在主机上的数组中,而不是正在连接的服务器中)。我无法在远程服务器上存储任何内容,也无法在该服务器上执行远程脚本。我只能通过 ssh 发出命令(有点愚蠢的设置)。目前,有多少个选项,它就可以连接多少次。这看起来效率很低。如果有人有更好的解决方案,我很想听听。
I currently have the following lines of code in a script:
set -A ARRAY OPTION1 OPTION2 OPTION3 OPTION4
set -A matches
for OPTION in ${ARRAY[@]}; do
DIFF=$(ssh $USER@$host " diff $PERSONALCONF $PRESETS$OPTION" )
if [[ $DIFF == "" ]]; then
set -A matches"${matches[@]}" $OPTION
fi
done
Basically, I have a loop that goes through each element in a pre-defined array, connects to a remote server (same server each time), and then compares a file with a file as defined by the loop using the diff command. Basically, it compares a personal.conf file with personal.conf.option1, personal.conf.option2, etc. If there is no difference, it adds it to the array. If there is a difference, nothing happens.
I was wondering if its possible to execute this or get the same result (storing the matching files in an array ON THE HOST MACHINE, not the server that's being connected to) by way of only connecting once via SSH. I cannot store anything on the remote server, nor can I execute a remote script on that server. I can only issue commands via ssh (kind of a goofy setup). Currently, it connects as many times as there are options. This seems inefficient. If anyone has a better solution I'd love to hear it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个选项:
您可以使用 OpenSSH 多路复用功能(请参阅
ssh(1)
)。此外,大多数 shell 都会很乐意接受通过 stdin 运行的脚本,因此您可以运行类似的内容
大多数脚本语言(Perl、Python、Ruby 等)都有一些 SSH 模块,允许连接重用:
Several options:
You can use OpenSSH multiplexing feature (see
ssh(1)
).Also, most shells will gladly accept a script to run over stdin, so you could just run something like
Most scripting languages (Perl, Python, Ruby, etc.) have some SSH module that allows connection reuse: