Execute the same command on multiple machines on a cluster because of actual development needs. For example, execute a specified script, delete files in the same directory (this is prudent ~ ~) and so on, the members of the group developed a ALLSCP (can execute the SCP command to synchronize the online code) very good, I also wrote a, The main is to use SSH to log on to a server, and then execute a specified command.
First paste the shell code:
#!/bin/bash
If ["$#"-ne 2]; then
echo "USAGE: $0-f server_list_file cmd"
exit-1
fi
file_name=$1< C5/>cmd_str=$2
cwd=$ (PWD)
CD $cwd
serverlist_file= "$CWD/$file _name"
if [!-e $serverlist _file]; Then
Echo ' server.list not exist ';
Exit 0
Fi while read line do
#echo $line
If [-N "$line"]; then
echo "Doing--->>> ;>> "$line" <<<<<<< "
ssh $line $cmd _str </dev/null >/dev/null
if [$-eq 0] ; Then
echo "$cmd _str done!"
else
echo "error:" $?
Fi
fi done
< $serverlist _file
The code is simple and does not explain.
How to use:
1. Create a new file host_file_list, file for the server's address, each row;
2. Save the above shell script, such as Save as allcmd.sh, pay attention to using chmod +x allcmd.sh make it executable script;
3. Run allcmd.sh host_file_list MD, Host_file_list is the 1th step of the file name (remember and allcmd.sh placed in the same directory), CMD is to execute the command, wrapped in single quotes, such as: Delete/home /nuaazdh/a tmp.txt file below: allcmd.sh host_file_list ' Rm/home/nuaazdh/tmp.txt '
4. Done.