shell编程-ssh免交互批量分发公钥脚本
脚本基本原理
1、控制端免交互创建秘钥和公钥:
1 ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
2、免交互发送公钥
1 sshpass -ppassword ssh-copy-id -i /root/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no user@172.25.0.21"
sshpass # 非交互式SSH密码提供
-o StrictHostKeyChecking=no # 不提示,ssh将自动添加新的主机密钥用户已知主机文件。
更多参数可以参考man ssh_config
ssh-copy-id # 本质上是调用ssh命令,进行远程拷贝公钥的一个脚本,其中值得关注的是脚本中的“shift”,它能够将传参的参数依次向前推进。
1 which ssh-copy-id
2 /usr/bin/ssh-copy-id
以下为shift在ssh-copy-id命令中使用的典型代码
3 if [ "-i" = "$1" ]; then
4 shift
5 # check if we have 2 parameters left, if so the first is the new ID file
6 if [ -n "$2" ]; then
7 if expr "$1" : ".*\.pub" > /dev/null ; then
8 ID_FILE="$1"
9 else
10 ID_FILE="$1.pub"
11 fi
12 shift # and this should leave $1 as the target name
13 fi
14 else
15 if [ x$SSH_AUTH_SOCK != x ] && ssh-add -L >/dev/null 2>&1; then
16 GET_ID="$GET_ID ssh-add -L"
17 fi
18 fi
以下为shift示例代码,能够加助理解shift将参数依次向前推进的含义
1 cat shift_test.sh
2 #!/bin/bash
3 until [ $# -eq 0 ];do
4 echo $*
5 shift
6 done
7 bash shift_test.sh 1 2 3 4 5
8 1 2 3 4 5
9 2 3 4 5
10 3 4 5
11 4 5
12 5
ssh免交互分发公钥的脚本
脚本功能:
1、能够输入选项 -h/--hlep查看帮助
2、不输入参数进行默认分发
3、可以指定主机的IP或者可以被解析的主机名进行分发
4、提示输出友好
5、能够自动检测已经分发了的主机,分发过了的就不再重复分发
6、代码尽量简洁
7、指定多个主机进行批量分发
效果示例1:帮助

效果示例2:默认分发、指定一个主机分发

效果示例3:指定多个主机同时进行批量分发

源码如下:
1 #!/bin/bash
2 # mzy 2019-09-22 Add Features
3 # another: 梅钟园 4 # contact QQ:359462962
5 export PATH=/bin:$PATH
6
7 # output command help manual
8 function output_help(){
9 echo -e "Usage :\n\n--help|-h\tget command help.\n\te.g:batchsent.sh --help\n\ncommand public key distribution:\n\t\e[40;32;1mbatchsent.sh [ip/hostname]\e[0;0;0m\n\nexample:\n\te.g:batchsent.sh 192.168.0.1\n\tor use default batchsent public key:\n\te.g:batchsent.sh\n\nexplanation:\n\t1.hostname needs to be able to be resolved IP address.\n\t2.Run this script need to have root privileges.\n\t3.The current system needs to be able to use yum install sshpass software."
10 }
11
12 # Check whether the IP address or host name of the obvious error
13 function check_ip_format(){
14 ip=$1
15 echo ${ip} |sed -r 's#([0-9]+).#\1#g' |test -n "`sed -n '/^[0-9][0-9]*$/p'`" >/dev/null 2>&1
16 if [ $? -eq 0 ];then
17 count=`echo ${ip}|sed -r 's#([0-9]+).#\1\n#g'|grep -v '^$' | wc -l`
18 if [ ${count} -eq 4 ];then
19 return 0
20 else
21 echo -e "\e[40;31;1merror\e[0;0;0m:this host(${ip}) ip---\e[40;31;1mThere are obvious errors\e[0;0;0m"
22 output_help
23 return 1
24 fi
25 else
26 ping -c 3 ${ip} >/dev/null 2>&1
27 if [ $? -eq 0 ];then
28 return 0
29 else
30 echo -e "\e[40;31;1merror\e[0;0;0m:this host(${ip}) name---\e[40;31;1mcan not be resolved\e[0;0;0m"
31 output_help
32 return 1
33 fi
34 fi
35 }
36
37 # Single IP or host public key distribution
38 function sent_pub_key(){
39 ip=$1
40 sshpass -prewqrewsdsds ssh "-o StrictHostKeyChecking=no" root@${ip} hostname >/dev/null 2>&1
41 if [ $? -eq 0 ];then
42 echo -e "${ip} \tpublic keys \e[40;34;1malready exist\e[0;0;0m,can be used normally."
43 else
44 ping -c 3 ${ip} >/dev/null 2>&1
45 if [ $? -eq 0 ];then
46 sshpass -ptemplate ssh-copy-id -i /root/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no root@${ip}" >/dev/null 2>&1
47 echo -e "${ip} \tpublic keys \e[40;32;1msent successfully\e[0;0;0m,can be used normally."
48 else
49 echo -e "${ip} \tthis host(${ip}) is \e[40;31;1mnot online\e[0;0;0m"
50 fi
51 fi
52 }
53
54 # define default host
55 function default_batch_sent_pub_key(){
56 for ip_addr in 172.16.0.{31,41,51,71,5,6,7,8,9};do
57 sent_pub_key ${ip_addr}
58 done
59 }
60
61 # default ip or host public key distribution
62 function batch_sent_pub_key(){
63 ip_addr=$1
64 sent_pub_key ${ip_addr}
65 }
66
67 # check the packages needed
68 function check_sshpass(){
69 if [ ! -f /usr/bin/sshpass ];then
70 yum install -y sshpass >/dev/null 2>&1
71 if [ $? -ne 0 ];then
72 echo -e "\e[40;31;1merror\e[0;0;0m:install sshpass failed,check to see if the current user has root privileges."
73 exit 1
74 fi
75 fi
76 }
77
78 # check -h or --help args
79 function check_help_args(){
80 args=$1
81 case ${args} in
82 "--help")
83 output_help
84 exit 1
85 ;;
86 "-h")
87 output_help
88 exit 1
89 ;;
90 esac
91 }
92
93 # The implementation of public key distribution by check_help_args function
94 # In this way the code is more complex, not recommended
95 function exec_batch_sent_by_check_help_args(){
96 check_help_args $1
97 if [ $# -eq 1 ];then
98 check_ip_format $1
99 if [ $? -eq 0 ];then
100 batch_sent_pub_key $1
101 fi
102 fi
103 }
104
105 # The implementation of public key distribution by if statment
106 # Such code simpler, recommended
107 function exec_batch_sent_by_if_statment(){
108 if [ $# -eq 1 ];then
109 if [ $1 == '--help' ] || [ $1 == '-h' ];then
110 output_help
111 else
112 check_ip_format $1
113 if [ $? -eq 0 ];then
114 batch_sent_pub_key $1
115 fi
116 fi
117 fi
118 }
119
120 # Check the generated keys
121 function check_the_generated_keys(){
122 if [ -f /root/.ssh/id_rsa -a -f /root/.ssh/id_rsa.pub ];then
123 return 0
124 else
125 ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""
126 if [ $? -eq 0 ];then
127 return 0
128 else
129 echo -e "\e[40;31;1merror\e[0;0;0m:install sshpass failed,check to see if the current user has root privileges."
130 return 1
131 fi
132 fi
133 }
134
135 # main
136 if [ $# -eq 0 ];then
137 check_sshpass
138 check_the_generated_keys
139 if [ $? -eq 0 ];then
140 default_batch_sent_pub_key
141 else
142 exit 1
143 fi
144 else
145 until [ $# -eq 0 ];do
146 check_sshpass
147 check_the_generated_keys
148 if [ $? -eq 0 ];then
149 exec_batch_sent_by_if_statment $1
150 else
151 exit 1
152 fi
153 shift
154 done
155 fi
shell编程-ssh免交互批量分发公钥脚本的更多相关文章
- ssh秘钥免交互批量分发脚本
将以下内容保存为.sh文件后运行即可,需根据各自情况修改ip_up和ip_arr #!/bin/bash #脚本功能:ssh秘钥免交互批量分发 #制 作 人:罗钢 联系方式:278554547@qqc ...
- shell编程之免交互 (不要再让你的双手过度劳累了)
shell编程之免交互 1.Here Document免交互 2.Expect免交互 3.免交互磁盘创建 1.Here Document免交互 概述: Here Document使用I/O重定向的方式 ...
- 8.shell编程之免交互
shell编程之免交互 目录 shell编程之免交互 Here Document免交互 免交互定义 Here Document变量设定 多行的注释 expect expect 定义 expect基本命 ...
- shell编程之免交互
目录: 一.Here Document 免交互 二.Expect 一.Here Document 免交互 使用I/O重定向的方式将命令列表提供给交互式程序或命令, 比如 ftp.cat 或 read ...
- expect--自动批量分发公钥脚本
1.在使用之前,先安装epel源,yum install expect -y2.写分发脚本,后缀为exp #!/usr/bin/expect set host_ip [lindex $argv 0] ...
- Shell批量SSH免交互登录认证
脚本实现功能:批量或单个SSH免交互登录认证 脚本应用场景:当部署集群时,大多数实现要配置好管理节点与从节点的SSH免交互登录,针对这样的情况,写了下面脚本,简化工作. 脚本支持系统:Ubuntu和C ...
- shell中expect免交互
expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...
- expect批量分发公钥
sshkey.exp #!/usr/bin/expect# 由于是多台服务器,需要在shell脚本中循环调用该脚本 if { $argc != 2 } { send_user "usage: ...
- Xshell配置ssh免密码登录-密钥公钥(Public key)与私钥(Private Key)登录【已成功实例】
本文转自https://blog.csdn.net/qjc_501165091/article/details/51278696 ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口 ...
随机推荐
- Autofac入门
注意:本文为原创文章,任何形式的转载.引用(包括但不限于以上形式)等,须先征得作者同意,否则一切后果自负. 简介 Autofac 是一个令人着迷的.NET IoC 容器. 它管理类之间的依赖关系.当应 ...
- Java Object类中toString方法的重写
Object类中的tostring方法的: 当我们输出一个对象时,实际是输出的是这个类中的tostring方法,是一个地址值,而不是类中的属性. 1 一:子类没有重写Object类中的toStrinn ...
- Air530Z GPS/北斗定位模块_设计指导手册_V1.2
下载PDF版本: Air530Z_定位模块_设计指导手册_V1.2.pdf @ 目录 1. 模块整体说明 2. 资料下载 3. 模块性能 4.模块管脚图 5.参考设计电路 6.GPS天线 6.1 无源 ...
- Java新一代单元测试框架JUnit5速览
为什么学JUnit5 Java技术栈的单元测试框架有两个:JUnit和TestNG,有种说法是TestNG比JUnit更强大,学TestNG就够了,但是当我打开GitHub看到star的时候,犹豫了: ...
- 微信获取信息发生错误(两个access_token的区别),错误代码:40001,说明:invalid credential, access_token is invalid or not latest hints
微信有两个access_token,一个是基础access_token,一个是网页授权access_token. 想要获取不带unionid的用户信息(以下链接)使用基础access_token ht ...
- 20204107 孙嘉临 《PYTHON程序设计》实验四报告
课程:<Python程序设计>班级: 2041姓名: 孙嘉临学号: 20204107实验教师:王志强实验日期:2020年6月29日必修/选修: 公选课 ##作为一个轻度游戏玩家,当然是要写 ...
- Linux-NFS存储
1.什么是NFS NFS是Network File System 的缩写,中文意思是网络文件共享系统,它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. 2.NFS存储服务 ...
- 5、基本数据类型(str)
5.1.字符串: 1.n1 = "lc" n2 = 'root' n3 = """chang""" n4='''tom' ...
- Sherlock and His Girlfriend题解
题目描述 Sherlock 有了一个新女友(这太不像他了!).情人节到了,他想送给女友一些珠宝当做礼物. 他买了 n件珠宝.第i 件的价值是i+1.那就是说,珠宝的价值分别为2,3,4,-,n+1 . ...
- (C++)戳西瓜
写在前面的话: 请不要吝啬你的点赞!!! 题目描述 有 n 个西瓜,编号为0 到 n-1,每个西瓜上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的西瓜.每当你戳破一个西瓜 i ...