1.编写脚本创建密钥及批量分发
#!/bin/bash
#批量分发的ip地址
ip_list="
152.16.1.5
152.16.1.7
152.16.1.8
152.16.1.9
152.16.1.51
152.16.1.31
152.16.1.82
"
echo '1.创建ssh-keygen'
#指定创建在家目录
ssh-keygen -f /root/.ssh/id_rsa -P ''
echo '2.分发 pub key'
for ip in $ip_list
do
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$ip
done
2.执行:sh /server/scripts/fenfa.sh
3.测试:ssh 152.16.1.5 ifconfig
4.编写脚本实现批量分发文件
#!/bin/sh
#调用函数库
. /etc/init.d/functions #调用函数库
#如果输出的参数小于两个就提示下面的话
if [ $# -ne 2 ]
then
echo "请输入两个参数"
exit 1
fi
#循环7和8
for n in 7 8
do
#拷贝文件到指定ip地址 #$n是上面循环的数字
scp -P 22 -rp $1 root@172.16.1.$n:$2 &>/dev/null
#如果返回值等于0则执行下面的命令
if [ $? -eq 0 ]
then
action "172.16.1.$n successful" /bin/true
else
action "172.16.1.$n failure" /bin/false
fi
done
测试
[root@m01 scripts]# sh file.sh
请输入两个参数
必须输入两个参数 第一个是发送文件的路径,第二个是接收文件的路径
[root@m01 scripts]# sh file.sh /etc /tmp/
172.16.1.7 successful [ 确定 ]
172.16.1.8 successful [ 确定 ]
成功