第一步,服務(wù)器準備
這里使用docker模擬幾臺服務(wù)器,分別命名為node2,node3,node4(使用鏡像chenqionghe/ubuntu,密碼統(tǒng)一為88888888),生產(chǎn)環(huán)境為ip或host
docker run -d --name node2 -p 2223:22 chenqionghe /ubuntu
docker run -d --name node3 -p 2224:22 chenqionghe /ubuntu
docker run -d --name node4 -p 2225:22 chenqionghe /ubuntu
還得有一臺主控制服務(wù)器node1,負責操作所有的服務(wù)器節(jié)點
docker run -d --name node1 -p 2222:22
--link node2:node2
--link node3:node3
--link node4:node4
chenqionghe/ubuntu
初始化完成后進入node1節(jié)點
ssh root@127.0.0.1 -p 2222
安裝必須軟件
apt-get install expect -y
創(chuàng)建存放腳本的目錄~/env
mkdir -p ~/env && cd ~/env
這里先模擬一個簡單的安裝包scripts/install.sh,安裝vim命令
mkdir scripts
cat > scripts/install.sh <<EOF
#!/bin/bash bash
apt-get installvim -y
EOF
創(chuàng)建機器列表配置文件,vim nodes.txt
node2
node3
node4
第二步 編寫自動化腳本
1.無交互ssh-keygen生成密鑰對腳本,vim ssh-keygen.exp
#!/usr/bin/expect
#set enter "
"
spawn ssh-keygen
expect {
"*(/root/.ssh/id_rsa)" {send "
";exp_continue}
"*(empty for no passphrase)" {send "
";exp_continue}
"*again" {send "
"}
}
expect eof
2.無交互分發(fā)公鑰腳本,vim send-sshkey.exp
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect send-sshkey.exp file host
"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "88888888"
spawn ssh-copy-id -i $file -p 22 root@$host
expect {
"yes/no" {send "yes
";exp_continue}
"*password" {send "$password
"}
}
expect eof
3.遠程批量執(zhí)行ssh腳本, vim mssh.sh
#!/bin/bash
Port=22
if [ $# -lt 1 ];then
echo "Usage: `basename $0` command"
exit
fi
echo $@
for v in `cat nodes.txt`
do
ssh -t -p ${Port} root@${v} $@
if [ $? -eq 0 ];then
echo "執(zhí)行成功:$v"
else
echo "執(zhí)行失?。?v"
fi
done
4.自動化部署腳本,vim auto-deploy.sh
#!/usr/bin/env bash
#機器列表
HostList=`cat nodes.txt`
#端口號
Port=22
# 1.無交互生成密鑰對
if [ ! -f ~/.ssh/id_rsa.pub ];then
expect ssh-keygen.exp
fi
# 2.無交互分發(fā)公密鑰
for v in ${HostList}
do
expect send-sshkey.exp ~/.ssh/id_rsa.pub ${v}
if [ $? -eq 0 ];then
echo "公鑰-發(fā)送成功:$v"
else
echo "公鑰-發(fā)送失?。?v"
fi
done
# 3.分發(fā)腳本文件(安裝軟件包)
for v in ${HostList}
do
scp -P ${Port} -rp ~/env/scripts root@${v}:~
if [ $? -eq 0 ];then
echo "scripts-發(fā)送成功:$v"
else
echo "scripts-發(fā)送失?。?v"
fi
done
# 4.使用腳本文件安裝
for v in ${HostList}
do
ssh -t -p ${Port} root@${v} /bin/bash ~/scripts/install.sh
done
到這里所有的文件已經(jīng)創(chuàng)建完成,整個env目錄結(jié)構(gòu)如下
├── auto-deploy.sh
├── mssh.sh
├── nodes.txt
├── scripts
│ └── install.sh
├── send-sshkey.exp
└── ssh-keygen.exp
第三步 執(zhí)行自動化腳本查看效果
sh auto-deploy.sh
執(zhí)行成功結(jié)果
spawn ssh-keygen
...
公鑰-發(fā)送成功:node2
...
公鑰-發(fā)送成功:node3
...
公鑰-發(fā)送成功:node4
install.sh 100% 40 41.4KB/s 00:00
scripts-發(fā)送成功:node2
install.sh 100% 40 45.0KB/s 00:00
scripts-發(fā)送成功:node3
install.sh 100% 40 39.9KB/s 00:00
scripts-發(fā)送成功:node4
...
Connection to node4 closed.
出現(xiàn)上面的結(jié)果代表3個node節(jié)點已經(jīng)初始化完成,拷貝公鑰+安裝軟件成功!
我們再來執(zhí)行一下遠程命令腳本,查看所有服務(wù)器系統(tǒng)類型
sh mssh.sh "cat /etc/os-release|head -n 1"
執(zhí)行結(jié)果如下
cat /etc/os-release|head -n 1
NAME="Ubuntu"
Connection to node2 closed.
執(zhí)行成功:node2
NAME="Ubuntu"
Connection to node3 closed.
執(zhí)行成功:node3
NAME="Ubuntu"
Connection to node4 closed.
執(zhí)行成功:node4
這樣就實現(xiàn)了自動化創(chuàng)建密鑰對+分發(fā)公鑰+所有服務(wù)器軟件環(huán)境安裝+批量遠程執(zhí)行腳本mssh.sh,麻雀雖小,五臟俱全,絕對是干貨!
本文摘自 :https://blog.51cto.com/u