【问题标题】:Select SCP Copy选择 SCP 副本
【发布时间】:2015-10-15 03:24:49
【问题描述】:

我看到了一个使用 RSYNC 和 SSH 来执行此操作的示例,但是在我的特定情况下,我无法使其正常工作 - 它给了我一些关于协议版本的错误

我想将类型为 .sh.jar.war 的所有文件从目录结构复制到 2 跳(带代理的 ssh)ssh 距离的远程主机。

我不确定执行此操作的最佳方法。是 SCP 还是某种 bash 脚本?

我知道的:

find . | grep "\.sh"

会给我一个很好的文件列表,我可能可以使用xargs 来处理它们。

我想一些选择是创建一个tar.gz 文件并在上面创建scp,或者编写一个小bash 脚本来单独进行SCP-ing?

我想出了用 perl 来做这件事的可怕方法,但它真的很慢

my $find = `find .`;

my $remote = "USER\@MACHINE:";

my $remotePrefix = "/devel/test";


my @files = split('\n', $find);

foreach (@files) {

    if (/(.*\/)([^\/]*\.sh)$/ or
        /(.*\/)([^\/]*\.jar)$/ or
        /(.*\/)([^\/]*\.war)$/
        ) {

        my $fullPath = $_;
        my $directory = $1;
        my $file = $2;
    

    # print "$fullPath => $directory   ||     $file\n";

    $cmd1 = qq(ssh USER\@MACHINE mkdir -p $remotePrefix$directory);
    $cmd2 = qq(scp $_ $remote$remotePrefix$_);


    `$cmd1`;
    `$cmd2`;
    }
}

【问题讨论】:

  • 你需要在远端创建匹配的目录结构吗?它还不存在?
  • 是的,我的脚本就是这样做的
  • 是的,我看到确实如此,但我正在检查是否有必要。该要求意味着您不能只为此使用 scp ,因为它不会那样做。这意味着如果你不能使用 rsync 的东西,tar 可能是最好的选择。
  • 您是否尝试在 rsync 上设置 --protocol=... 以匹配遥控器?
  • @meuh 在绝大多数情况下,协议不匹配错误与rsync 协议无关,通常是由远程shell 的配置文件在登录非交互式shell 时打印内容引起的。

标签: linux bash perl ssh scp


【解决方案1】:

假设您的文件名不包含换行符,您可以使用cpio(1)

find . -type f \( -name '*.sh' -o -name '*.[jw]ar' \) -print | \
    cpio -oa | \
    ssh "${USER}@${MACHINE}" "cd ${remotePrefix}; cpio -idm"

【讨论】:

    【解决方案2】:

    有点旧的帖子,但我刚刚有一个类似的任务,经过一些研究在 bash 中提出了这个,使用 sshpass 允许内联 ssh 使用,基本目录存在于本地和远程服务器上。新的文件目录将在远程服务器上创建。这被称为:script fid src-base-dir

    #!/bin/bash
    
    fid=$1;  
    basedir=$2;
    
    ##myfiles are fixed 10 numeric files stored in respective directories starting at the 6th pos
    ##eg dir 1234560000 for files 1234560000.file - 1234569999.file
    
    dir=${fid:0:6}0000;     ## modify for your directory type or use other shell substring  
    fpath=$dir/$fid.file;   ## here you could expand bash to specify extensions or all eg $dir/$fid.{csv,jpg,js}
    
    if [ -a "$basedir/$fpath" ]
    then
        rDir=$(sshpass -e ssh -p <myport> <user>@<server> "ls '$basdir/$dir' 2>&1 > /dev/null ");
        if [[ ! -z $rDir ]];        
        then
            sshpass -e ssh -p <myport> <user>@<server> "mkdir '$basedir/$dir'";
            sshpass -e scp -P <myport> $basedir/$fpath <user>@<server>:"$basedir/$fpath";
        else
            sshpass -e scp -P <myport> $basedir/$fpath <user>@<server>:"$basedir/$fpath";
        fi
    else
        echo $basedir/$fpath no exist!!
    fi
    

    【讨论】:

      【解决方案3】:

      使用Net::SFTP::Foreign:

      use Net::SFTP::Foreign;
      my $sftp = Net::SFTP::Foreign->new($host, user => $user);
      $sftp->rput('.', $remote_prefix,
                  wanted => qr/\.(?:sh|war|jar)$/);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 2018-03-11
        • 2021-03-12
        • 2015-11-02
        • 1970-01-01
        相关资源
        最近更新 更多