Parallel SSH 批量操作集群服务器

发布于 2022-10-15 08:26:21 字数 6711 浏览 20 评论 0

Parallel SSH 批量操作集群服务器

PSSH安装下载:

  1. # wget http://parallel-ssh.googlecode.com/files/pssh-2.2.2.tar.gz
  2. # tar zxvf pssh-2.2.2.tar.gz
  3. # cd pssh-2.2.2
  4. # python setup.py install  //先安装python2.6再安装.

复制代码复制代码

如果是采用密钥登陆,需要输入密的话,可以利用ssh-agent输入一次密码即可:

  1. #ssh-auto.sh
  2. #start agent
  3. killall -9 ssh-agent
  4. eval `ssh-agent`
  5. ssh-add ./kvman.pri

复制代码复制代码

  1. # cat hosts-file
  2. p1
  3. p2
  4. # pssh -h hosts-file -l ben date
  5. [1] 21:12:55 [SUCCESS] p2 22
  6. [2] 21:12:55 [SUCCESS] p1 22
  7. # pssh -h hosts-file -l ben -P date
  8. p2: Thu Oct 16 21:14:02 EST 2008
  9. p2: [1] 21:13:00 [SUCCESS] p2 22
  10. p1: Thu Sep 25 15:44:36 EST 2008
  11. p1: [2] 21:13:00 [SUCCESS] p1 22

复制代码复制代码

  1. Normally the standard output from the remote hosts is not shown to you. The -P option in the last invocation displays the output from both remote hosts as well as the exit status. If you are running more complex commands you might like to use -i instead to see each remote host's output grouped nicely under its hostname rather than mixed up as the output comes in from the hosts. You can also use the --outdir pssh option to specify the path of a directory that should be used to save the output from each remote host. The output for each host is saved in separate file named with the remote machine's hostname.
  2. You can use the --timeout option to specify how long a command can take. It defaults to 60 seconds. This means that if your command fails to complete within 60 seconds on a host, pssh will consider it an error and report it as such, as shown below. You can increase the timeout to something well above what might be acceptable (for example to 24 hours) to avoid this problem.
  3. # pssh -h hosts-file -l ben -i "sleep 65; date"
  4. [1] 21:19:26 [FAILURE] p1 22 Timeout
  5. [2] 21:19:26 [FAILURE] p2 22 (4, 'Interrupted system call')

复制代码复制代码

  1. The pscp command takes the same -h, -l, and --timeout options and includes a --recursive option to enable deep copying from the local host. At the end of the command you supply the local and remote paths you would like to copy.  The first pscp command in the example below copies a single file to two remote hosts in parallel. The following ssh command checks that the file exists on the p1 machine. The second pscp command fails in a verbose manner without really telling you the simple reason why. Knowing that I was trying to copy a directory over, I added the --recursive option to the command and it executed perfectly. The final ssh command verifies that the directory now exists on the p1 remote host.
  2. $ mkdir  example-tree
  3. $ date > example-tree/df1.txt
  4. $ date > example-tree/df2.txt
  5. $ mkdir  example-tree/subdir1
  6. $ date > example-tree/subdir1/df3.txt
  7. $ pscp -h hosts-file -l ben example-tree/df1.txt /tmp/df1.txt
  8. [1] 21:28:36 [SUCCESS] p1 22
  9. [2] 21:28:36 [SUCCESS] p2 22
  10. $ ssh p1 "cat /tmp/df1.txt"
  11. Thu Oct 16 21:27:25 EST 2008
  12. $ pscp -h hosts-file -l ben example-tree /tmp/example-tree
  13. ...
  14. python: Python/ceval.c:2918: set_exc_info: Assertion `frame != ((void *)0)' failed.
  15. Aborted
  16. $ pscp -h hosts-file -l ben --recursive  example-tree /tmp/example-tree
  17. [1] 21:29:57 [SUCCESS] p1 22
  18. [2] 21:29:57 [SUCCESS] p2 22
  19. $ ssh p1 "ls -l /tmp/example-tree"
  20. total 24
  21. -rw-r--r-- 1 ben ben   29 2008-09-25 16:01 df1.txt
  22. -rw-r--r-- 1 ben ben   29 2008-09-25 16:01 df2.txt
  23. drwxr-xr-x 2 ben ben 4096 2008-09-25 16:01 subdir1

复制代码复制代码

  1. The prsync command uses only a handful of the command-line options from rsync. In particular, you cannot use the verbose or dry-run options to get details or see what would have been done. The command shown below will rsync the example-tree into /tmp/example-tree on the remote hosts in a manner similar to the final command in the pscp example.
  2. $ prsync -h hosts-file -l ben -a --recursive  example-tree /tmp

复制代码复制代码

  1. The main gain of the prsync command over using the normal rsync command with pssh is that prsync gives a simpler command line and lets you sync from the local machine to the remote hosts directly. Using pssh and rsync, you are running the rsync command on each remote machine, so the remote machine will need to connect back to the local machine in order to sync.
  2. The pslurp command is sort of the opposite to the pscp in that it grabs a file or directory off all the remote machines and copies it to the local machine.  The below command grabs the example-tree directory from both p1 and p2 and stores them into /tmp/outdir. The -r option is shorthand for --recursive. As you can see, for each remote host a new directory is created with the name of the host, and inside that directory a copy of example-tree is made using the local directory name supplied as the last argument to pslurp.
  3. # mkdir /tmp/outdir
  4. # pslurp -h hosts-file -L /tmp/outdir -l ben -r /tmp/example-tree example-tree
  5. # l /tmp/outdir
  6. drwxr-xr-x 3 root root 4.0K 2008-10-16 21:47 p1/
  7. drwxr-xr-x 3 root root 4.0K 2008-10-16 21:47 p2/
  8. # l /tmp/outdir/p2
  9. drwxr-xr-x 3 root root 4.0K 2008-10-16 21:47 example-tree/
  10. # l /tmp/outdir/p2/example-tree/
  11. -rw-r--r-- 1 root root   29 2008-10-16 21:47 df10.txt
  12. -rw-r--r-- 1 root root   29 2008-10-16 21:47 df1.txt
  13. ...
  14. drwxr-xr-x 2 root root 4.0K 2008-10-16 21:47 subdir1/
  15. 复制代码

复制代码You can use environment variables to make things easier with Parallel ssh. You can use the PSSH_HOSTS variable to name the hosts file instead of using the -h option. Likewise, the PSSH_USER environment variable lets you set the username to log in as, like the -l pssh command line option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小霸王臭丫头 2022-10-22 08:26:21

看得人不少,咋没有人回复呢!

胡渣熟男 2022-10-22 08:26:21

和我的博客里实现的功能差不多啊,我的是用expect{:3_182:}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文