每日一shell(十一) 批量快速推送key到远程主机

作用:快速自动生成密钥,并批量推送到远程主机,适用于为ansible环境批量推送key

#!/bin/bash

keypath=/root/.ssh
iplist=/root/ip.txt
[[ -e ${iplist} ]] && > ${iplist}
for i in `seq 81 91`
do
   echo -E "192.168.118.$i" >> ${iplist}
   ssh-keyscan 192.168.118.$i >> ${keypath}/known_hosts
done
cat ${iplist}
[ -d {keypath} ] || mkdir -p ${keypath}
rpm -q expect &> /dev/null || yum install expect -y
[[ -e ${keypath}/id_rsa.pub ]] || ssh-keygen -t rsa -f ${keypath}/id_rsa -P ""
password=u7wtiYTl0
while read ip;do
expect << EOF
set timeout 5
spawn ssh-copy-id $ip
expect {

“yes/no” { send "yes\n";exp_continue }
"password" { send "$password\n"  }

}
expect off
EOF
done < ${iplist}

原文地址:http://blog.51cto.com/m51cto/2156563

时间: 2024-08-03 09:28:00

每日一shell(十一) 批量快速推送key到远程主机的相关文章

自动化运维Ansible批量部署服务+shell脚本批量推送公钥

一.概述分析 由于互联网的快速发展导致产品更新换代速度逐渐加快,运维人员每天都要进行大量的维护操作,仍旧按照传统方式进行维护会使得工作效率低下.这时,部署自动化运维就可以尽可能安全.高效地完成这些工作.一般会把自动化运维工具划分为两类:一类是需要使用代理工具的,也就是基于专用的ABem程序来完成管理功能,如: Puppet.Func. Zabbix等:另外一类是不需要配置代理工具的,可以直接基于SSH服务来完成管理功能,如: Ansible. Fabric等. - 下面介绍几款功能类似的自动化运

自动化运维工具ansible学习+使用ansible批量推送公钥到远程主机

目录: 一.ansible简介 1.1.ansible是什么 1.2.ansible如何工作 1.3.ansible优缺点 1.4.ansible安装方式 1.5.ansible文件简单介绍 1.6.ansible简单使用 二.ansible-shell简介 2.1.ansible-shell是什么 2.2.ansible-shell安装方式 2.3.ansible-shell简单使用 三.使用ansible的authorized_key模块批量推送公钥到远程主机 3.1.生产秘钥对 3.2.使

每日算法之十一:Integer to Roman

题目:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 罗马表示方式如下: I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000; 其中每两个阶段的之间有一个减法的表示,比如900=CM, C写在M前面表示M-C. 范围给到3999,感觉情况不多直接打表其实更快,用代码判断

如何加速推送SCCM Agent到客户端机器

当企业管理员部署完System Center Configuration Manager时,第一时间应该是考虑将SCCM Agent程序推送到客户端机器以便实现之后的应用程序分发.软件更新.资产管理等一系列设置,但是由于SCCM的默认设置经常会造成推送Agent程序会很慢,尤其是在客户端数量很多的情况下.今天主要介绍如何加速推送SCCM Agent到客户端机器. 首先部署一套SCCM服务器在您的企业环境中 设置客户端推送账户以及推送账户,见文章:管理Configuration Manager客户

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

Shell脚本批量创建用户并随机生成密码

要求:批量创建10个系统账号oldboy01-oldboy10,并设置生成密码(密码不同). 实现脚本: #!/bin/bash #Question3 for i in $(seq -w 10) do         useradd -s /bin/bash oldboy$i         echo "password$i" | md5sum | tee -a passwd.txt | passwd --stdin  oldboy$i done 脚本执行效果: [[email pro

每日算法之三十一:Combination Sum

给定一个整数序列,求解一个子序列,子序列之和等于给定目标值.子序列满足以下条件: 1)子序列是有序的 2)子序列的元素个数不限,可以是给定元素的重复元素. 3)结果中的子序列是唯一的 原题描述如下: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeat

Linux系统编写shell脚本批量创建和删除用户

一.编写shell脚本批量添加用户 实现方法:判断用户是否存在,存在则返回错误提示,同时判断用户文件是否存在,不存在则退出 1.创建添加用户脚本 [[email protected] ~]# vim useradd.sh #!/bin/bashif [ $# -eq 0 ];then        echo "你没有输入任何文件!"        exit 1fi if [ ! -f $1 ];then        echo "输入有误!"        exit

shell脚本批量收集linux服务器的硬件信息快速实现

安装ansible批量管理系统.(没有的话,ssh远程命令循环也可以) 在常用的数据库里面新建一张表,用你要收集的信息作为列名,提供可以用shell插入. 批量安装所需工具包,smartmontools.dmidecode.mysql,可以用ansible命令:ansible all_host -s -m yum -a "name=mysql" 在本地写脚本. 获取默认IP:default_ip=$(ifconfig|head -n 2|tail -n 1|cut -d ":