使用shell脚本批量分发密钥及管理

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                                      [  确定  ]

成功
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值