#!/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#传送文件