Parallel SSH 批量操作集群服务器
Parallel SSH 批量操作集群服务器
PSSH安装下载:
- # wget http://parallel-ssh.googlecode.com/files/pssh-2.2.2.tar.gz
- # tar zxvf pssh-2.2.2.tar.gz
- # cd pssh-2.2.2
- # python setup.py install //先安装python2.6再安装.
复制代码复制代码
如果是采用密钥登陆,需要输入密的话,可以利用ssh-agent输入一次密码即可:
- #ssh-auto.sh
- #start agent
- killall -9 ssh-agent
- eval `ssh-agent`
- ssh-add ./kvman.pri
复制代码复制代码
- # cat hosts-file
- p1
- p2
- # pssh -h hosts-file -l ben date
- [1] 21:12:55 [SUCCESS] p2 22
- [2] 21:12:55 [SUCCESS] p1 22
- # pssh -h hosts-file -l ben -P date
- p2: Thu Oct 16 21:14:02 EST 2008
- p2: [1] 21:13:00 [SUCCESS] p2 22
- p1: Thu Sep 25 15:44:36 EST 2008
- p1: [2] 21:13:00 [SUCCESS] p1 22
复制代码复制代码
- 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.
- 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.
- # pssh -h hosts-file -l ben -i "sleep 65; date"
- [1] 21:19:26 [FAILURE] p1 22 Timeout
- [2] 21:19:26 [FAILURE] p2 22 (4, 'Interrupted system call')
复制代码复制代码
- 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.
- $ mkdir example-tree
- $ date > example-tree/df1.txt
- $ date > example-tree/df2.txt
- $ mkdir example-tree/subdir1
- $ date > example-tree/subdir1/df3.txt
- $ pscp -h hosts-file -l ben example-tree/df1.txt /tmp/df1.txt
- [1] 21:28:36 [SUCCESS] p1 22
- [2] 21:28:36 [SUCCESS] p2 22
- $ ssh p1 "cat /tmp/df1.txt"
- Thu Oct 16 21:27:25 EST 2008
- $ pscp -h hosts-file -l ben example-tree /tmp/example-tree
- ...
- python: Python/ceval.c:2918: set_exc_info: Assertion `frame != ((void *)0)' failed.
- Aborted
- $ pscp -h hosts-file -l ben --recursive example-tree /tmp/example-tree
- [1] 21:29:57 [SUCCESS] p1 22
- [2] 21:29:57 [SUCCESS] p2 22
- $ ssh p1 "ls -l /tmp/example-tree"
- total 24
- -rw-r--r-- 1 ben ben 29 2008-09-25 16:01 df1.txt
- -rw-r--r-- 1 ben ben 29 2008-09-25 16:01 df2.txt
- drwxr-xr-x 2 ben ben 4096 2008-09-25 16:01 subdir1
复制代码复制代码
- 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.
- $ prsync -h hosts-file -l ben -a --recursive example-tree /tmp
复制代码复制代码
- 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.
- 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.
- # mkdir /tmp/outdir
- # pslurp -h hosts-file -L /tmp/outdir -l ben -r /tmp/example-tree example-tree
- # l /tmp/outdir
- drwxr-xr-x 3 root root 4.0K 2008-10-16 21:47 p1/
- drwxr-xr-x 3 root root 4.0K 2008-10-16 21:47 p2/
- # l /tmp/outdir/p2
- drwxr-xr-x 3 root root 4.0K 2008-10-16 21:47 example-tree/
- # l /tmp/outdir/p2/example-tree/
- -rw-r--r-- 1 root root 29 2008-10-16 21:47 df10.txt
- -rw-r--r-- 1 root root 29 2008-10-16 21:47 df1.txt
- ...
- drwxr-xr-x 2 root root 4.0K 2008-10-16 21:47 subdir1/
- 复制代码
复制代码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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看得人不少,咋没有人回复呢!
和我的博客里实现的功能差不多啊,我的是用expect{:3_182:}