批量给Linux服务器分发公钥
来源:互联网 发布:手机剪辑软件 编辑:程序博客网 时间:2024/05/16 19:00
1.公钥的生成
[root@nfs1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
27:68:04:bf:45:8d:b7:44:a4:a9:18:38:49:07:b7:b4 root@http1
The key's randomart image is:
+--[ RSA 2048]----+
| o.= .=o |
| . * = ..o+ |
| + E o +o . |
| . + = . |
| . = S . |
| . o |
| |
| |
| |
+-----------------+
2.安装分发使用的软件
[root@nfs1 ~]# yum install expect -y
3.编辑exp文件
[root@nfs1 ~]# vim fenfa_sshkey.exp
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "MyPassword"
spawn ssh-copy-id root@$host:$dir
#spawn ssh-copy-id -i $file "-p 52113 oldboy@$host:$dir"
"*password" {send "$password\r"}
}
expect eof
exit -onexit {
send_user "Oldboy say good bye to you!\n"
}
#script usage
#expect oldboy-6.exp file host dir
#example
#./oldboy-6.exp /etc/hosts 10.0.0.179 /etc/hosts
exit -onexit {
send_user "Oldboy say good bye to you!\n"
exit -onexit {
send_user "Oldboy say good bye to you!\n"
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
~
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
4.编辑分发使用的脚本并给执行的权限
[root@nfs1 ~]# vim fenfa_sshkey.sh
vi fenfa_sshkey.sh
#!/bin/sh
. /etc/init.d/functions
for ip in `cat iplist`
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip ~/ >/dev/null 2>&1
if [ $? -eq 0 ];then
action "$ip" /bin/true
else
action "$ip" /bin/false
fi
done
[root@nfs1 ~]# chmod +x fenfa_sshkey.sh
5.编译要分发的主机ip地址表
[root@nfs1 ~]# vim iplist
10.1.41.60
10.1.41.61
10.1.41.62
10.1.41.63
10.1.41.64
10.1.41.65
10.1.41.66
10.1.41.67
10.1.41.68
10.1.41.69
10.1.41.71
6.检查所需的文件完整性
[root@nfs1 ~]# ll
-rw-r--r-- 1 root root 1352 3月 15 20:43 fenfa_sshkey.exp
-rwxr-xr-x 1 root root 218 3月 15 20:38 fenfa_sshkey.sh
-rw-r--r-- 1 root root 121 3月 15 20:44 iplist
7.执行脚本分发ssh-key
[root@nfs1 ~]# sh fenfa_sshkey.sh
10.1.41.60 [ 确定 ]
10.1.41.61 [ 确定 ]
10.1.41.62 [ 确定 ]
10.1.41.63 [ 确定 ]
10.1.41.64 [ 确定 ]
10.1.41.65 [ 确定 ]
10.1.41.66 [ 确定 ]
10.1.41.67 [ 确定 ]
10.1.41.68 [ 确定 ]
10.1.41.69 [ 确定 ]
10.1.41.71 [ 确定 ]
ok 完成公钥的分发
[root@nfs1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
27:68:04:bf:45:8d:b7:44:a4:a9:18:38:49:07:b7:b4 root@http1
The key's randomart image is:
+--[ RSA 2048]----+
| o.= .=o |
| . * = ..o+ |
| + E o +o . |
| . + = . |
| . = S . |
| . o |
| |
| |
| |
+-----------------+
2.安装分发使用的软件
[root@nfs1 ~]# yum install expect -y
3.编辑exp文件
[root@nfs1 ~]# vim fenfa_sshkey.exp
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "MyPassword"
spawn ssh-copy-id root@$host:$dir
#spawn ssh-copy-id -i $file "-p 52113 oldboy@$host:$dir"
"*password" {send "$password\r"}
}
expect eof
exit -onexit {
send_user "Oldboy say good bye to you!\n"
}
#script usage
#expect oldboy-6.exp file host dir
#example
#./oldboy-6.exp /etc/hosts 10.0.0.179 /etc/hosts
exit -onexit {
send_user "Oldboy say good bye to you!\n"
exit -onexit {
send_user "Oldboy say good bye to you!\n"
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
~
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
4.编辑分发使用的脚本并给执行的权限
[root@nfs1 ~]# vim fenfa_sshkey.sh
vi fenfa_sshkey.sh
#!/bin/sh
. /etc/init.d/functions
for ip in `cat iplist`
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip ~/ >/dev/null 2>&1
if [ $? -eq 0 ];then
action "$ip" /bin/true
else
action "$ip" /bin/false
fi
done
[root@nfs1 ~]# chmod +x fenfa_sshkey.sh
5.编译要分发的主机ip地址表
[root@nfs1 ~]# vim iplist
10.1.41.60
10.1.41.61
10.1.41.62
10.1.41.63
10.1.41.64
10.1.41.65
10.1.41.66
10.1.41.67
10.1.41.68
10.1.41.69
10.1.41.71
6.检查所需的文件完整性
[root@nfs1 ~]# ll
-rw-r--r-- 1 root root 1352 3月 15 20:43 fenfa_sshkey.exp
-rwxr-xr-x 1 root root 218 3月 15 20:38 fenfa_sshkey.sh
-rw-r--r-- 1 root root 121 3月 15 20:44 iplist
7.执行脚本分发ssh-key
[root@nfs1 ~]# sh fenfa_sshkey.sh
10.1.41.60 [ 确定 ]
10.1.41.61 [ 确定 ]
10.1.41.62 [ 确定 ]
10.1.41.63 [ 确定 ]
10.1.41.64 [ 确定 ]
10.1.41.65 [ 确定 ]
10.1.41.66 [ 确定 ]
10.1.41.67 [ 确定 ]
10.1.41.68 [ 确定 ]
10.1.41.69 [ 确定 ]
10.1.41.71 [ 确定 ]
ok 完成公钥的分发
0 0
- 批量给Linux服务器分发公钥
- 用shell脚本批量分发公钥,实现用…
- Linux服务器批量管理工具
- dsh批量管理linux服务器
- pxe批量部署linux服务器
- Linux 批量修改服务器密码
- Linux Cluster环境下批量分发执行补丁
- Linux下实现SSH Key 批量分发管理
- ssh批量分发数据
- 给服务器装linux系统
- 利用expect批量部署linux服务器
- PXE 远程批量安装linux 服务器
- [基础]RHEL6下LINUX服务器批量部署
- 通过dsh批量管理Linux服务器
- Linux集群管理工具pssh批量管理服务器
- 通过dsh批量管理Linux服务器
- Linux集群管理工具PSSH批量管理服务器
- 实现linux 服务器自动批量化管理
- javaSE路径问题
- 简单工厂和抽象工厂
- C++11之std--future和std--promise
- Linux—用户管理相关文件
- Spring boot构建RESTFul API+使用Swagger2构建API文档
- 批量给Linux服务器分发公钥
- 字节流_FileInputStream类读取
- CSS2.1 17个命名颜色
- OpenGL核心技术之SSAO讲解(四)
- 怎么判断一组数字是不是堆
- poj 3258 最大化最小值
- 建立ftp服务器
- 字节流_FileOutputStream类写出
- Nginx介绍和安装准备