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

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

 下面是来自老男孩培训初级第14期第六节课的批量分发管理服务器课程的刘同学的作品

这是一个无需做任何配置一键就实现批量分发密钥和文件的脚本:

#!/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

同期女同学作品

轻松实现远程批量拷贝文件脚本(女学生作品)

转载地址:http://heoml.baihongyu.com/

你可能感兴趣的文章
iframe 无刷新上传图片以及返回值
查看>>
PHP_crack_check
查看>>
servlet提供下载图片服务
查看>>
shiro 在方法上添加注解配置
查看>>
NAT网络穿透
查看>>
SEO的艺术(原书第2版)
查看>>
ResouceBundle读取properties文件
查看>>
Spotlight-like Hint View
查看>>
NDRotator
查看>>
ubuntu安装pycharm,以及创建桌面快捷方式
查看>>
像写SQL一样编写Java数据应用-TinySqlDsl
查看>>
FreeCMS视频教程 用户管理
查看>>
关于header的那些事——header location重定向语句对页面进行跳转失败的原因
查看>>
汉语智能分词
查看>>
Se(2)---对浏览器操作
查看>>
mycncart 之网银在线chinapay支付方式
查看>>
Flink 读取Kafka写入Hive
查看>>
Junit 源码解析(一)
查看>>
pycurl mac 安装报错Curl is configured to use SSL,
查看>>
服务器部署多个tomcat方法
查看>>