When deploying a distributed system, there will be the following scenarios: an application is deployed on N Linux hosts. Before the project is deployed, it is necessary to verify whether the JDK version or other resource configuration of all hosts meets expectations, for example, check the JDK versions of all machines Are they the same.
If you log in to the hosts one by one, and then use the command "java -version" to verify each one, although the task can be completed, the efficiency is low. To improve efficiency, multiple hosts can execute commands in batches through scripts. The implementation steps are as follows.
One, Linux host settings ssh password-free login
First, the Linux host must be set up for ssh secret-free login. One of them can be used as a trusted host to log in to all other hosts without secret. For the specific configuration method, see " Configuring the trust relationship between two Linux hosts (and how to cancel) ".
Two, create a host list file hosts
The vi hosts command creates a host list file hosts and lists all remote hosts in the file.
$ vi hosts
$ cat hosts
47.100.247.242
101.132.242.27
47.103.217.188
Three, create doCommand.sh script
#!/bin/sh
doCommand()
{
hosts=`sed -n '/^[^#]/p' hosts`
for host in $hosts
do
echo ""
echo HOST $host
ssh $host "$@"
done
return 0
}
if [ $# -lt 1 ]
then
echo "$0 cmd"
exit
fi
doCommand "$@"
echo "return from doCommand"
Fourth, add execution permissions to the script file doCommand.sh
$ chmod u+x doCommand.sh
5. Run script commands on trusted hosts
The format of the script running command is as follows:
./doCommand.sh "[yourcommand]"
Note: The parameter [yourcommand] is the command to be executed on the trusted host.
Examples are as follows:
$ ./doCommand.sh "java -version"
HOST 47.100.247.242
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-b10)
OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)
HOST 101.132.242.27
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
HOST 47.103.217.188
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
return from doCommand
In this way, the script will execute the "java -version" command on each machine and return the JDK version information of each host to the trusted host.
Article reference: