简述

如果你想批量启动整个集群的某个服务,你不可能挨个切换终端软件去执行命令,你会累死,所以就写个脚本,在主服务器去执行命令,这样的话每个机器都可以同时执行这个命令了.

开始编写脚本

xcall文件

  1. #!/bin/bash
  2. #在集群的所有机器上批量执行同一条命令
  3. if(($#==0))
  4. then
  5. echo 请输入您要操作的命令!
  6. exit
  7. fi
  8. echo 要执行的命令是$*
  9. #循环执行此命令
  10. #我服务器名字是 zjj101 zjj102 zjj103 这里调整你们自己的服务器名字.
  11. for((i=101;i<=103;i++))
  12. do
  13. echo ---------------------zjj$i-----------------
  14. ssh zjj$i $*
  15. done

hosts文件配置

我的hosts配置,我把三台服务器分别配置了 zjj101 zjj102 zjj103 这三台,
如果你不会配置hosts的话,参考: https://blog.csdn.net/qq_41489540/article/details/116400827

[root@zjj101 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6



172.16.10.101 zjj101
172.16.10.102 zjj102
172.16.10.103 zjj103

配置ssh免密码登录

生成公钥和私钥

生成公钥和私钥 , 默认保存括号里面的内容,

命令: ssh-keygen -t rsa

直接回车三下就可以了,啥也不用管

[root@zjj101 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:HLoOUb25W6avx5qhdKUmvR0Zh8lyKGbTir8x7z2lPTU root@zjj101.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|       .         |
|      . o        |
|     . + * o     |
|    . * S B .    |
|     = * * +. E  |
|    o B B.=+ . . |
|     = O @=.o    |
|      =oX=+. .   |
+----[SHA256]-----+
[root@zjj101 ~]#

将公钥复制到远程机器中

命令: ssh-copy-id root@zjj101

敲完了之后需要舒若zjj101的密码,

解释: root@zjj101是要复制的目标机器

下面是操作步骤:

执行下面的命令,没有先后顺序执行完了之后需要输入目标机器的登录密码:

ssh-copy-id root@zjj102

ssh-copy-id root@zjj103

ssh-copy-id root@zjj101

[root@zjj101 .ssh]# ssh-copy-id root@zjj102
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already i                                           nstalled
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install th                                           e new keys
root@zjj102's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@zjj102'"
and check to make sure that only the key(s) you wanted were added.

[root@zjj101 .ssh]#  ssh-copy-id root@zjj103
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already i                                           nstalled
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install th                                           e new keys
root@zjj103's password:

Number of key(s) added: 1

[root@zjj101 ~]#  ssh-copy-id root@zjj101
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already i                                           nstalled
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install th                                           e new keys
root@zjj101's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@zjj101'"
and check to make sure that only the key(s) you wanted were added.

更详细的介绍看这里: https://blog.csdn.net/qq_41489540/article/details/109091590

配置环境变量

上面脚本已经编写完了,集群多机器ssh免秘钥也配置好了,但是现在还有一个缺陷,执行命令必须要在脚本所在的目录下, 也就是说我xcall脚本在A目录,那么我执行命令也必须先 cd进入到A目录,才能执行, 如果不在脚本所在的目录下的话,会提示: 没有那个文件或目录

[root@zjj101 etc]# sh xcall jps
sh: xcall: 没有那个文件或目录

解决思路是将脚本所在的文件夹配置到环境变量中 , “/root/script/“是我脚本所在的目录

修改 “/etc/profile” 文件 , 将 /root/script/ 目录加入到环境变量中,

命令:

vi /etc/profile

profile文件内容:

export PATH="$PATH:/root/script/"
export PATH

更新下环境变量:

命令:

 source /etc/profile

其它说明可以看这个: https://zjj1994.blog.csdn.net/article/details/120801535

开始使用

查看 java 进程 : sh xcall jps

# 如果你不输入命令的话,会提示 "请输入您要操作的命令!"
[root@zjj101 etc]# sh xcall 
请输入您要操作的命令!

# 输入jps命令
[root@zjj101 etc]# sh xcall jps
要执行的命令是jps
---------------------zjj101-----------------
17098 Jps
---------------------zjj102-----------------
bash: jps: 未找到命令
---------------------zjj103-----------------
2188 Jps
[root@zjj101 etc]#

查看集群每台服务器的名字

sh xcall hostname

[root@zjj101 etc]# sh xcall hostname
要执行的命令是hostname
---------------------zjj101-----------------
zjj101.localdomain
---------------------zjj102-----------------
zjj102.localdomain
---------------------zjj103-----------------
zjj103.localdomain
[root@zjj101 etc]#

换个目录依然能执行这个命令

[root@zjj101 etc]# cd /
[root@zjj101 /]# sh xcall hostname
要执行的命令是hostname
---------------------zjj101-----------------
zjj101.localdomain
---------------------zjj102-----------------
zjj102.localdomain
---------------------zjj103-----------------
zjj103.localdomain
[root@zjj101 /]#

进一步优化,不输入sh命令也能执行

上面 “sh xcall jps “ , “sh xcall hostname “ ,前面都有带个 sh , 这样感觉有点别扭 = =.

解决办法就是添加权限 “chmod +x /root/script/xcall” ,当然这个看个人爱好,如果在公司上,肯定是不让随便添加权限的.如果自己玩玩,那无所谓了.

# 不输入sh ,直接输入xcall jsp , 发现报错了,
[root@zjj101 etc]# xcall jps
-bash: /root/script/xcall: 权限不够
# 添加权限
[root@zjj101 etc]#  chmod +x /root/script/xcall
# 测试效果
[root@zjj101 etc]# xcall jps
要执行的命令是jps
---------------------zjj101-----------------
16902 Jps
---------------------zjj102-----------------
bash: jps: 未找到命令
---------------------zjj103-----------------
2165 Jps

错误解决

如果执行shell出现 行4: 未预期的符号 `$‘\r‘‘ 附近有语法错误 错误,看下面的链接
添加链接描述