歡迎您光臨本站 註冊首頁

Parallel SSH 批量操作集群伺服器

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

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
21:12:55 p2 22
21:12:55 p1 22

# pssh -h hosts-file -l ben -P date
p2: Thu Oct 16 21:14:02 EST 2008
p2: 21:13:00 p2 22
p1: Thu Sep 25 15:44:36 EST 2008
p1: 21:13:00 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"
21:19:26 p1 22 Timeout
21:19:26 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
21:28:36 p1 22
21:28:36 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
21:29:57 p1 22
21:29:57 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.
《解決方案》

看得人不少,咋沒有人回復呢!
《解決方案》

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

[火星人 ] Parallel SSH 批量操作集群伺服器已經有839次圍觀

http://coctec.com/docs/service/show-post-5066.html