本文共 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/