1056  
查询码:00000137
ssh秘钥批量分发脚本
作者: wyasw 于 2020年03月29日 发布在分类 / Linux / 常用shell脚本 下,并于 2020年03月29日 编辑
shell

ssh秘钥批量分发脚本

sh ssh_key.sh 172.16.105.117 172.16.105.118 172.16.105.119 172.16.105.120 172.16.105.113

vim ssh_key.sh

#!/bin/bash

#Blog:http://blog.wyasw.com
#Time:2017-02-03 14:17:15
#Name:ssh_key.sh
#bcription:SSH
密钥批量分发
User=root
passWord=GIChain123@2018
function YumBuild(){
yum -y clean all &>/dev/null
yum makecache &>/dev/null
}

echo "
正在进行网络连接测试,请稍后..."
ping www.baidu.com -c2 >/dev/null ||(echo "
无法连同外网,本脚本运行环境必须和外网相连!" && exit)
[ $# -eq 0 ] && echo "
没有参数!格式为:sh $0 参数IP1...n" && exit 
rpm -q sshpass &>/dev/null || yum -y install sshpass &>/dev/null
if [ $? -gt 0 ];then
    YumBuild
    yum -y install sshpass &>/dev/null || (echo "sshpass build error
" && exit)
fi
[ -d ~/.ssh ] || mkdir ~/.ssh;chmod 700 ~/.ssh
echo "
正在创建密钥对...."
rm -rf ~/.ssh/id_dsa ~/.ssh/id_dsa.pub
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P "" &>/dev/null
for ip in $*
do
    ping $ip -c1 &>/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




 推荐知识

 历史版本

修改日期 修改人 备注
2020-03-29 21:11:52[当前版本] wyasw 创建版本

  目录
    文艺知识分享平台 -V 4.9.5 -wcp
    京公网安备100012199188号 京ICP备2021030911号