有没有办法并行处理多个系统的scp?


9

是否可以同时并行地将文件复制到所有系统?就像@一次,应该在所有机器上复制文件...

Answers:


9

有一种方法。 看看这个 Ubuntu手册页。

名称

   parallel-scp - parallel versions of scp

概要

   parallel-scp [OPTIONS] -h hosts.txt local remote

描述

   pssh provides a number of commands for executing against a group of
   computers, using SSH. It's most useful for operating on clusters of
   homogenously-configured hosts.

   parallel-scp copy files in parallel to a set of machines.

资料来源:Ubuntu手册页


6
这里的一些例子会很棒!
豪尔赫·卡斯特罗

parallel-scp:找不到命令
Jonathan

2

是的,要安装parallel-scp,必须安装软件包pssh:

sudo apt install pssh 

然后将显示命令(请参阅man parallel-scp):

man parallel-scp

NAME
       parallel-scp — parallel process kill program

SYNOPSIS
       parallel-scp [-vAr] [-h hosts_file] [-H [user@]host[:port]] [-l user] [-p par]
       [-o outdir] [-e errdir] [-t timeout] [-O options] [-x  args]  [-X  arg]  local
       remote

DESCRIPTION
       parallel-scp  is a program for copying files in parallel to a number of hosts.
       It provides features such as passing a  password  to  scp,  saving  output  to
       files, and timing out.

OPTIONS
       -h host_file
       --hosts host_file
              Read hosts from the given host_file.  Lines in the host file are of the
              form [user@]host[:port] and can include blank lines and comments (lines
              beginning  with  "#").  If multiple host files are given (the -h option
              is used more than once), then  parallel-scp  behaves  as  though  these
              files  were  concatenated  together.   If  a host is specified multiple
              times, then parallel-scp will connect the given number of times.

       -H     [user@]host[:port]
       --host [user@]host[:port]
       -H     "[user@]host[:port] [ [user@]host[:port ] ... ]"
       --host "[user@]host[:port] [ [user@]host[:port ] ... ]"
              Add the given host strings to the list of hosts.  This  option  may  be
              given  multiple  times,  and  may  be  used  in conjunction with the -h
              option.

       -l user
       --user user
              Use the given username as the default for any host entries  that  don't
              specifically specify a user.

       -p parallelism
       --par parallelism
              Use the given number as the maximum number of concurrent connections.

       -t timeout
       --timeout timeout
              Make  connections  time  out after the given number of seconds.  With a
              value of 0, parallel-scp will not timeout any connections.

       -o outdir
       --outdir outdir
              Save standard output to files in the given directory.  Filenames are of
              the  form  [user@]host[:port][.num]  where  the  user and port are only
              included for hosts that explicitly  specify  them.   The  number  is  a
              counter that is incremented each time for hosts that are specified more
              than once.

       -e errdir
       --errdir errdir
              Save standard error to files in the given directory.  Filenames are  of
              the same form as with the -o option.

       -x args
       --extra-args args
              Passes  extra  SSH  command-line arguments (see the ssh(1) man page for
              more information about SSH arguments).  This option  may  be  specified
              multiple  times.   The  arguments are processed to split on whitespace,
              protect text within quotes, and escape with backslashes.  To pass argu‐
              ments without such processing, use the -X option instead.

       -X arg
       --extra-arg arg
              Passes  a single SSH command-line argument (see the ssh(1) man page for
              more information about SSH arguments).  Unlike the -x option,  no  pro‐
              cessing  is  performed  on  the argument, including word splitting.  To
              pass multiple command-line arguments, use  the  option  once  for  each
              argument.

       -O options
       --options options
              SSH  options  in the format used in the SSH configuration file (see the
              ssh_config(5) man page for more information).  This option may be spec‐
              ified multiple times.

       -A
       --askpass
              Prompt for a password and pass it to ssh.  The password may be used for
              either to unlock a key or for password authentication.  The password is
              transferred  in  a  fairly  secure manner (e.g., it will not show up in
              argument lists).  However, be aware that a root  user  on  your  system
              could potentially intercept the password.

       -v
       --verbose
              Include error messages from ssh with the -i and \ options.

       -r
       --recursive
              Recursively copy directories.

我认为您的答案不完整。您展示了如何安装工具,但没有演示如何使用它。这里没有回答这个问题。
Eskander Bejaoui,

1

假设您的所有目标计算机都具有正确的ssh密钥配置,因此您无需输入密码即可登录,那么这样做的方法是:

#!/bin/bash
FILE="/put/your/file/here.txt"
TARGET_PATH="/where/to/put/on/remote/system/"
MACHINES="machine1 machine2 machine3"

for current_machine in $MACHINES; do 
   scp $FILE $current_machine:$TARGET_PATH
done

它不会并行执行,而是一个接一个地执行;但是,您无需单独跟踪每个命令,它们都将自动运行。


这是我一直在做的,尽管是单线的:) ... +1(即使不是并行的)
0xC0000022L 2012年

如果您像@NiteRain建议的那样放一个&后面scp $FILE $current_machine:$TARGET_PATH,那基本上不是并行执行吗?
Helper Method

是的,应该可以。
roadmr 2014年

0

让我举例说明如何使用此命令:

  1. 要安装:sudo apt install pssh这将使您可以使用以下命令:

    • parallel-scp:并行进程终止程序
    • parallel-ssh:并行ssh程序
    • parallel-rsync:并行进程终止程序
    • parallel-slurp:并行进程终止程序
    • parallel-nuke:并行进程终止程序
  2. 用法基于以下格式:

    parallel-scp [-vAr] [-h hosts_file] [-H [user @] host [:port]] [-l用户] [-p par] [-o outdir] [-e errdir] [-t时间- out] [-O选项] [-x args] [-X arg] [-L localdir]远程本地

    • 使用示例:

      1. 创建一个hosts将从中pssh读取主机名的文件,该文件的格式为:

        • [user@]host[:port],因此george@192.168.3.30:333

          Each should be on a different line and comments are preceeded by a `#` character
          
      2. 实际复制命令:

        • parallel-scp -h /path/to/hosts/file src dest
        • 要在所有服务器上复制$ HOME / demo.txt tp / tmp /:

          parallel-scp -h /path/to/hosts/file $HOME/demo.txt /tmp/
          

奖金

  1. 在所有服务器上运行命令:

    parallel-ssh -i -h /path/to/hosts/file uptime
    
  2. 在所有服务器上终止进程:

    parallel-nuke -h /path/to/hosts/file firefox
    

资料来源:https : //www.cyberciti.biz/cloud-computing/how-to-use-pssh-parallel-ssh-program-on-linux-unix/

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.