083-使用shell和expect一鍵批量分發SSH密鑰腳本

#!/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/functions
file="$1"
remote_dir="$2"
if [[ $# -ne 2 ]];then
echo  "usage:$0 argv2"
echo "must have one argvs"
exit
fi
function KNOWN_HOST_REBUILD()
{
#確保本機存在known_hosts列表
[ ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ && touch ~/.ssh/known_hosts
local i=$1
sed -i "/^${i} /d" ~/.ssh/known_hosts
expect -c "
spawn /usr/bin/ssh oldboy@${i} echo ok;
expect \"*yes/no)?\";
send \"yes\r\";
expect eof " >/dev/null 2>&1
return 0
[[ $? -ne 0 ]] && echo "$i know host rebuild fail,maybe the server connect error"
}
function PASS_PASSWD()
{
ip=$1
expect -c "
set timeout -1
spawn ssh-copy-id -i id_dsa oldboy@$ip
expect \"*password:\"
send \"oldboy123\r\"
expect eof" >/dev/null 2>&1
}
function FENFA_id_dsa()
{
for ip in `awk '/^[^#]/{print $1}' all_client.txt`
do
KNOWN_HOST_REBUILD $ip
PASS_PASSWD $ip
if [[ $? -eq 0 ]];then
action "$ip send id_dsa is successful" /bin/true
else
action "$ip send id_dsa is failed copied" /bin/false
fi
done
}
function FENFA_config()
{
for ip in `awk '/^[^#]/{print $1}' all_client.txt`
do
port=$(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>&1
if [[ $? -eq 0 ]];then
action "$ip send $file is successful!!" /bin/true
else
action "$ip send $file is failed!!" /bin/false
fi
done
}
FENFA_id_dsa
FENFA_config

 

#!/bin/bash
# this scripts comes from oldboy trainning's student.
. /etc/init.d/functions
file="$1"
#本地傳送的文件
remote_dir="$2"
#傳送到其它服務器的文件,若是是/tmp目錄則不授權限控制
if [[ $# -ne 2 ]];then
echo  "usage:$0 argv2"
echo "must have one argvs"
exit
fi
#傳參,若是不是2個參數就退出腳本
function KNOWN_HOST_REBUILD()
{

[ ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ && touch ~/.ssh/known_hosts
#判斷~/.ssh/known_hosts 是文件 ,而且建立文件known_hosts
local 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=$1
expect -c "
set timeout -1
spawn ssh-copy-id -i id_dsa oldboy@$ip
expect \"*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文件中出了#號開頭的第一行
do
KNOWN_HOST_REBUILD $ip
#創建know_hosts 中的密鑰文件
PASS_PASSWD $ip
#分發送密鑰
if [[ $? -eq 0 ]];then
action "$ip send id_dsa is successful" /bin/true
else
action "$ip send id_dsa is failed copied" /bin/false
fi
done
}
function FENFA_config()
{
for ip in `awk '/^[^#]/{print $1}' all_client.txt`
do
port=$(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 ]];then
action "$ip send $file is successful!!" /bin/true
else
action "$ip send $file is failed!!" /bin/false
fi
done
}
FENFA_id_dsa
# 分發密鑰
FENFA_config
#傳送文件
相關文章
相關標籤/搜索