linux脚本之批量同步、多机命令执行和机器免密脚本

1、机器之间免密脚本

改脚本旨在简化机器之间进行免密的操作,更加流程化。

编辑脚本:vim ssh.sh

#!/bin/bash
​
# 定义存储目标主机信息的文件路径
HOSTS_FILE="hosts.txt"
​
# 检查本地是否已经存在SSH密钥对,如果不存在则生成
if [ ! -f ~/.ssh/id_rsa ]; then
    ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
fi
​
# 检查主机信息文件是否存在
if [ ! -f $HOSTS_FILE ]; then
    echo "主机信息文件 $HOSTS_FILE 不存在,请创建该文件并添加主机信息"
    exit 1
fi
​
# 循环读取主机信息文件中的每一行
while IFS= read -r line; do
    # 从行中提取主机名和用户名
    HOST=$(echo $line | awk '{print $1}')
    USER=$(echo $line | awk '{print $2}')
    if [ -z "$USER" ]; then
        USER="root"
    fi
​
    # 尝试将本地公钥复制到目标主机
    ssh-copy-id -i ~/.ssh/id_rsa.pub $USER@$HOST
    if [ $? -eq 0 ]; then
        echo "成功配置 $USER@$HOST 的免密登录"
    else
        echo "配置 $USER@$HOST 的免密登录失败"
    fi
done < "$HOSTS_FILE"

为脚本添加可执行权限:chmod 755 ssh.sh

脚本中有用到机器之间的ip(或映射)和用户名。

所以编辑hosts文件:vim hosts.txt

hadoop01 root
hadoop02 root
hadoop03 root

eg:

# 在hadoop01节点执行脚本,为hadoop01节点对hadoop02和hadoop03节点的免密操作
[root@hadoop01 ~]# ./ssh.sh 
Generating public/private rsa key pair.
Created directory '/root/.ssh'.
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:exLYoTi0HM+HyrtIpXErTMpMIxbucR2fRtytVM/VMkE root@hadoop01
The key's randomart image is:
+---[RSA 2048]----+
|           . .Eo.|
|      . . o o + .|
| .  o. o.o . o o |
|. .o.*+=o..      |
|.*o.B.*+S.       |
|B++* +.. o       |
|.+= +   o .      |
| . o .   o       |
|  . o.           |
+----[SHA256]-----+
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'hadoop01 (192.168.15.111)' can't be established.
ECDSA key fingerprint is SHA256:mspiDUMYXm2W1i2YO5+ZLiaofWSUY60ZRy5USg7UcwE.
ECDSA key fingerprint is MD5:dd:2a:24:c2:66:09:59:c8:b4:e5:1e:81:e5:7f:38:70.
# 此处需要输入yes,并按下回车键
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
# 此处需要输入hadoop01机器的密码并按下回车键
root@hadoop01's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh 'root@hadoop01'"
and check to make sure that only the key(s) you wanted were added.
​
成功配置 root@hadoop01 的免密登录
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'hadoop02 (192.168.15.112)' can't be established.
ECDSA key fingerprint is SHA256:mspiDUMYXm2W1i2YO5+ZLiaofWSUY60ZRy5USg7UcwE.
ECDSA key fingerprint is MD5:dd:2a:24:c2:66:09:59:c8:b4:e5:1e:81:e5:7f:38:70.
# 此处需要输入yes,并按下回车键
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
# 此处需要输入hadoop01机器的密码并按下回车键
root@hadoop02's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh 'root@hadoop02'"
and check to make sure that only the key(s) you wanted were added.
​
成功配置 root@hadoop02 的免密登录
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'hadoop03 (192.168.15.113)' can't be established.
ECDSA key fingerprint is SHA256:mspiDUMYXm2W1i2YO5+ZLiaofWSUY60ZRy5USg7UcwE.
ECDSA key fingerprint is MD5:dd:2a:24:c2:66:09:59:c8:b4:e5:1e:81:e5:7f:38:70.
# 此处需要输入yes,并按下回车键
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
# 此处需要输入hadoop01机器的密码并按下回车键
root@hadoop03's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh 'root@hadoop03'"
and check to make sure that only the key(s) you wanted were added.
​
成功配置 root@hadoop03 的免密登录

此时就可以在hadoop01机器上面对hadoop01、hadoop02和hadoop03的机器进行免密登陆。如果要

2、多机分发脚本

此脚本旨在便捷的将一个或多个文件递归分发到其他机器。

首先编辑分发脚本:vim scp.sh

#!/bin/bash
echo "----------开始执行----------"
#从ips文件中读取ip列表
ips=$(cat ips | awk '{print $1}')
# 接收最后一个参数作为目标目录
dir=${!#}
# 除最后一个参数外,其余均为要传输的文件
files=("${@:1:$#-1}")
​
# 循环读取每个ip
for ip in ${ips}    
do
    for file in "${files[@]}"
    do
        exec="scp -r $file root@$ip:$dir"
        echo $exec
        # 执行命令并判断结果
        if ! eval $exec; then
            echo "Failed $file to $host:$dir. Exiting..."
            exit 1
        fi
    done
done
echo "All files have been successfully transferred to all hosts."
echo "----------执行结束----------"

给脚本添加执行权限:chmod 755 scp.sh

跟多机命令执行脚本一样脚本需要机器的ip或映射,编辑ips文件:vim ips,内容如下。

# 以下几个是机器的主机映射
hadoop01
hadoop02
hadoop03

eg:

[root@hadoop01 ~]# ./scp.sh scp.sh ips ssh.sh hosts.txt /root/
----------开始执行----------
scp -r scp.sh root@hadoop01:/root/
scp.sh                                                                                                         100%  673   881.4KB/s   00:00    
scp -r ips root@hadoop01:/root/
ips                                                                                                            100%   27    49.3KB/s   00:00    
scp -r ssh.sh root@hadoop01:/root/
ssh.sh                                                                                                         100%  948     1.5MB/s   00:00    
scp -r hosts.txt root@hadoop01:/root/
hosts.txt                                                                                                      100%   42    49.2KB/s   00:00    
scp -r scp.sh root@hadoop02:/root/
scp.sh                                                                                                         100%  673   318.1KB/s   00:00    
scp -r ips root@hadoop02:/root/
ips                                                                                                            100%   27     9.8KB/s   00:00    
scp -r ssh.sh root@hadoop02:/root/
ssh.sh                                                                                                         100%  948   339.9KB/s   00:00    
scp -r hosts.txt root@hadoop02:/root/
hosts.txt                                                                                                      100%   42    16.8KB/s   00:00    
scp -r scp.sh root@hadoop03:/root/
scp.sh                                                                                                         100%  673   364.6KB/s   00:00    
scp -r ips root@hadoop03:/root/
ips                                                                                                            100%   27    57.6KB/s   00:00    
scp -r ssh.sh root@hadoop03:/root/
ssh.sh                                                                                                         100%  948   136.4KB/s   00:00    
scp -r hosts.txt root@hadoop03:/root/
hosts.txt                                                                                                      100%   42    23.3KB/s   00:00    
All files have been successfully transferred to all hosts.
----------执行结束----------

3、多机命令执行脚本

此脚本旨在方便一次执行多台机器上面同一个命令,比如:

命令:./all.sh ll /

解释:all.sh是脚本,ll /是我们要在多台机器上面执行的命令

首先编辑脚本文件:vim all.sh,内容如下。

#!/bin/bash
echo "----------开始执行----------"
#从ips文件中读取ip列表
ips=$(cat ips | awk '{print $1}')
# 接收传递的指令(参数)
cmd=$*
# 循环读取每个ip
for ip in ${ips}
do
    exec="ssh $ip $cmd"
    echo $exec
    #执行命令并判断是否执行成功
    if eval $exec; then
        echo "success"
    else
        echo "fail"
    fi
done
echo "----------执行结束----------"

为脚本添加执行权限:chmod 755 all.sh

在脚本中会用到需要执行命令的机器的ip或映射,因为上面已经编辑了ips文件所以本次不用。

如果只需要此脚本,且ips文件没有的话,则参考上面的ips格式(一样的)编写一份ips文件即可。

eg:

[root@hadoop01 ~]# ./all.sh ls /root/
----------开始执行----------
ssh hadoop01 ls /root/
all.sh
anaconda-ks.cfg
hosts.txt
ips
mysql57-community-release-el7-10.noarch.rpm
scp.sh
soft
ssh.sh
success
ssh hadoop02 ls /root/
all.sh
anaconda-ks.cfg
hosts.txt
ips
mysql57-community-release-el7-10.noarch.rpm
scp.sh
soft
ssh.sh
success
ssh hadoop03 ls /root/
all.sh
anaconda-ks.cfg
hosts.txt
ips
mysql57-community-release-el7-10.noarch.rpm
scp.sh
soft
ssh.sh
success
----------执行结束----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲸语少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值