linux下SSH服务利用shell脚本实现密钥的批量分发与执行

SSH项目利用shell脚本实现密钥的批量分发与执行

1 ssh密钥的批量分发

开始安装sshpass免交互工具并进行SSH-key的批量分发

1.1 下载epel源并更新yum仓库

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y clean all
yum makecache

1.2 安装sshpass工具

yum -y install sshpass

2 创建密钥文件

2.1 免交互创建密钥对

root@ansible-1:~#:ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ""
Generating public/private dsa key pair.
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:Ey9bwNHOjrvyfNvNoJCAbUa9OZXrmy8xxB+DYMAz0JU root@ansible-1
The key's randomart image is:
+---[DSA 1024]----+
| .+.ooo |
| =oE... |
| .+==o. |
| + B*.o |
| . = S++. o |
| o ..O+ . |
| +..o. |
| ....o= + |
| o+o=+o o |
+----[SHA256]-----+

# 查看是否创建成功
root@ansible-1:~#:ll .ssh/
total 12
-rw------- 1 root root 751 Apr 28 16:04 id_dsa
-rw-r--r-- 1 root root 604 Apr 28 16:04 id_dsa.pub
-rw-r--r-- 1 root root 515 Apr 27 15:56 known_hosts

2.2 参数说明


ssh-keygen:生成密钥对命令
-t:指定密钥对的密码加密类型(rsa,dsa两种)
-f:指定密钥对文件的生成路径包含文件名
-P(大写):指定密钥对的密码(密语)

程序同时要求输入一个密语字符串(passphrase),空表示没有密语(主机密钥的密语必须为空)。
密语和口令(password)非常相似,但是密语可以是一句话,里面有单词、标点符号、数字、空格或任何你想要的字符。
好的密语要30个以上的字符,难以猜出,由大小写字母、数字、非字母混合组成。密语可以用 -p 选项修改。
丢失的密语不可恢复。如果丢失或忘记了密语,用户必须产生新的密钥,然后把相应的公钥分发到其他机器上去。

2.3 支持的非对称加密算法

1.rsa,基于大数的质数分解难度的算法,一般都是2048位
2.dsa,基于离散对数的难度,1024位
3.ecdsa,椭圆曲线算法,支持256,384,521位

3 免交互方式批量分发公钥

#!/bin/bash
#    create key pair
rm -fr /root/.ssh/id_dsa*
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P "" -q
# fenfa key file
for ip in 31 41
do
echo "=======Batch the file to the host 172.16.1.$ip=========="    
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.$ip"
echo -e "##########################END##########################\n"
done

 

参考:https://blog.csdn.net/qq_34672033/article/details/89645010

posted @ 2019-10-19 01:30  老虎逛大街  阅读(971)  评论(0编辑  收藏  举报
levels of contents