SSH密钥的批量分发
原创
©著作权归作者所有:来自51CTO博客作者互联网小阿祥的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.wget获取epel源
wget http://files.tttidc.com/centos6/epel-6.repo
mv epel-6.repo /etc/yum.repos.d/
2.检查本地挂载情况,如果没挂载,须先挂载
挂载命令:mount /dev/sr0 /mnt #挂在路径根据你们自己本地的yum源来挂载
#清空缓存
yum clean all
#创建缓存
yum makecache
3.下载sshpass软件包
4.创建密钥对文件
ssh-keygen -t dsa #指定dsa算法
5.免交互方式发放公钥
sshpass -p "123456" ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no root@192.168.10.91
6.测试免密登录
7.编写ssh密钥对免交户批量分发脚本
#!/bin/bash
#author:lixiang
#2021-09-13
#定义变量
user=root
password=123456
function YumBuild(){
echo "正在安装epel源yum仓库..."
cd /etc/yum.repos.d/
wget http://files.tttidc.com/centos6/epel-6.repo &>/dev/null
yum clean all &>/dev/null
yum makecache &>/dev/null
}
echo "测试主机ip是否能连接外网..."
ping www.baidu.com -c 2 >/dev/null || (echo "无法连接外网,请检查本地网络环境" && exit)
[ $# -eq 0 ] && echo "没有参数!格式为:sh $0 " && exit
YumBuild
if [ $? -gt 0 ];then
echo "epel源构建失败..." && exit
fi
rpm -q sshpass &>/dev/null || yum -y install sshpass &>/dev/null || (echo "sshpass build error!" && exit)
[ -d ~/.ssh ] || mkdir ~/.ssh
chmod 700 ~/.ssh
echo "正在创建密钥对..."
ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa &>/dev/null
for ip in $*
do
ping $ip -c 2 &>/dev/null
if [ $? -gt 0 ];then
echo "$ip无法ping通网络"
continue
fi
sshpass -p "$password" ssh-copy-id -i ~/.ssh/id_dsa.pub -o StrictHostKeyChecking=no ${user}@$ip &>/dev/null
echo "$ip 公钥分发成功"
done
8.测试公钥发放
chmod u+x ssh.sh #赋予执行权限
./ssh.sh 192.168.10.92 192.168.10.93 192.168.10.94 192.168.10.95 192.168.10.96