在 KSH 中循环 SSH 的更有效方法?

发布于 2024-11-17 15:49:05 字数 759 浏览 2 评论 0原文

我目前在脚本中有以下几行代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能否归途做我良人 2024-11-24 15:49:05

几个选项:

  • 您可以使用 OpenSSH 多路复用功能(请参阅 ssh(1))。

  • 此外,大多数 shell 都会很乐意接受通过 stdin 运行的脚本,因此您可以运行类似的内容

    cat script.sh | ssh $HOST /bin/sh
    
  • 大多数脚本语言(Perl、Python、Ruby 等)都有一些 SSH 模块,允许连接重用:

    #!/usr/bin/perl
    使用 Net::OpenSSH;
    我的($用户,$主机)=(...);
    我的@options = (...);
    我的@matches;
    我的 $ssh = Net::OpenSSH->new("$user\@$host");  
    对于我的 $option (@options) {
        我的 $diff = $ssh->capture("diff $personal_conf $presets$option");
        if ($ssh->错误) {
            警告“命令失败:”。 $ssh->错误;
        }
        别的 {
            推@matches,$ option if $ diff eq '';
        }
    }
    打印“@matches\n”;
    

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

    cat script.sh | ssh $HOST /bin/sh
    
  • Most scripting languages (Perl, Python, Ruby, etc.) have some SSH module that allows connection reuse:

    #!/usr/bin/perl
    use Net::OpenSSH;
    my ($user, $host) = (...);
    my @options = (...);
    my @matches;
    my $ssh = Net::OpenSSH->new("$user\@$host");  
    for my $option (@options) {
        my $diff = $ssh->capture("diff $personal_conf $presets$option");
        if ($ssh->error) {
            warn "command failed: " . $ssh->error;
        }
        else {
            push @matches, $option if $diff eq '';
        }
    }
    print "@matches\n";
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文