博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
083-使用shell和expect一键批量分发SSH密钥脚本
阅读量:7113 次
发布时间:2019-06-28

本文共 3501 字,大约阅读时间需要 11 分钟。

hot3.png

#!/bin/bash# this scripts comes from oldboy trainning's student.# e_mail:70271111@qq.com# qqinfo:49000448# function: remote dis ssh key.# version:1.1################################################# oldboy trainning info.# QQ 80042789 70271111# site:http://www.etiantian.org# blog:http://oldboy.blog.51cto.com# oldboy trainning QQ group: 208160987 45039636################################################. /etc/init.d/functionsfile="$1"remote_dir="$2"if [[ $# -ne 2 ]];thenecho  "usage:$0 argv2"echo "must have one argvs"exitfifunction KNOWN_HOST_REBUILD(){#确保本机存在known_hosts列表[ ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ && touch ~/.ssh/known_hostslocal i=$1sed -i "/^${i} /d" ~/.ssh/known_hostsexpect -c "spawn /usr/bin/ssh oldboy@${i} echo ok;expect \"*yes/no)?\";send \"yes\r\";expect eof " >/dev/null 2>&1return 0[[ $? -ne 0 ]] && echo "$i know host rebuild fail,maybe the server connect error"}function PASS_PASSWD(){ip=$1expect -c "set timeout -1spawn ssh-copy-id -i id_dsa oldboy@$ipexpect \"*password:\"send \"oldboy123\r\"expect eof" >/dev/null 2>&1}function FENFA_id_dsa(){for ip in `awk '/^[^#]/{print $1}' all_client.txt`doKNOWN_HOST_REBUILD $ipPASS_PASSWD $ipif [[ $? -eq 0 ]];thenaction "$ip send id_dsa is successful" /bin/trueelseaction "$ip send id_dsa is failed copied" /bin/falsefidone}function FENFA_config(){for ip in `awk '/^[^#]/{print $1}' all_client.txt`doport=$(grep $ip all_client.txt|awk '{print $2}')scp -P${port} -r -p ${file} oldboy@${ip}:~ >/dev/null 2>&1 && \ssh -p${port} -t oldboy@$ip sudo rsync ~/`basename ${file}` $remote_dir >/dev/null 2>&1if [[ $? -eq 0 ]];thenaction "$ip send $file is successful!!" /bin/trueelseaction "$ip send $file is failed!!" /bin/falsefidone}FENFA_id_dsaFENFA_config

 

#!/bin/bash# this scripts comes from oldboy trainning's student.. /etc/init.d/functionsfile="$1"#本地传送的文件remote_dir="$2"#传送到其它服务器的文件,如果是/tmp目录则不受权限控制if [[ $# -ne 2 ]];thenecho  "usage:$0 argv2"echo "must have one argvs"exitfi#传参,如果不是2个参数就退出脚本function KNOWN_HOST_REBUILD(){[ ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ && touch ~/.ssh/known_hosts#判断~/.ssh/known_hosts 是文件 ,并且创建文件known_hostslocal i=$1#声明变量i的值sed -i "/^${i} /d" ~/.ssh/known_hosts#如果known_hosts 中有10.0.x.x  开头的密钥,则删除expect -c "#命令分发spawn /usr/bin/ssh oldboy@${i} echo ok;#嵌套执行命令并输出OK ,用expect语句expect \"*yes/no)?\";send \"yes\r\";expect eof " >/dev/null 2>&1#>/dev/null 2>&1  输出到黑洞里面去return 0#返回值为0则正常,1则不正常[[ $? -ne 0 ]] && echo "$i know host rebuild fail,maybe the server connect error"}#提示你如果输出不为0,则错误function PASS_PASSWD(){ip=$1expect -c "set timeout -1spawn ssh-copy-id -i id_dsa oldboy@$ipexpect \"*password:\"send \"oldboy123\r\"expect eof" >/dev/null 2>&1}#分发公钥去服务器function FENFA_id_dsa(){for ip in `awk '/^[^#]/{print $1}' all_client.txt`#选取all_client.txt文件中出了#号开头的第一行doKNOWN_HOST_REBUILD $ip#建立know_hosts 中的密钥文件PASS_PASSWD $ip#分发送密钥if [[ $? -eq 0 ]];thenaction "$ip send id_dsa is successful" /bin/trueelseaction "$ip send id_dsa is failed copied" /bin/falsefidone}function FENFA_config(){for ip in `awk '/^[^#]/{print $1}' all_client.txt`doport=$(grep $ip all_client.txt|awk '{print $2}')取端口号scp -P${port} -r -p ${file} oldboy@${ip}:~ >/dev/null 2>&1 && \#scp 传送文件去oldboy的家目录中ssh -p${port} -t oldboy@$ip sudo rsync ~/`basename ${file}` $remote_dir >/dev/null 2>&1#文件过去了,开始移动文件if [[ $? -eq 0 ]];thenaction "$ip send $file is successful!!" /bin/trueelseaction "$ip send $file is failed!!" /bin/falsefidone}FENFA_id_dsa# 分发密钥FENFA_config#传送文件

 

转载于:https://my.oschina.net/u/3635512/blog/1640345

你可能感兴趣的文章
Execute to Parse %: 29.76,数据库硬解析过高,监控告警提示数据库硬解析比例过低...
查看>>
三、Hadoop命令
查看>>
moment.js笔记
查看>>
2.1 Word 插入 smartart、图表
查看>>
css+div的存在问题
查看>>
PoEdu - C++阶段班【Po学校】- Lesson03-4_构造函数&赋值函数&拷贝构造函数&学习方式 - 第6天...
查看>>
通过url给action传中文参数乱码解决方案
查看>>
糊涂窗口综合症
查看>>
Hadoop的调度器总结(转)
查看>>
Linux磁盘空间被未知资源耗尽
查看>>
英语基础
查看>>
核心动画 - CAKeyframeAnimation 简单应用
查看>>
html的marquee理解
查看>>
区块链中的密码学
查看>>
大数据第七天
查看>>
放新浪微博的箭头css写法
查看>>
H5视频推流方案
查看>>
struts2获得需要的文件或者访问路径
查看>>
python 读写CSV文件
查看>>
Linux tar解压出错
查看>>