1.在其中一台服务器创建文件 ip.txt IP配置文件
备注: IP 用户名 密码 端口 (注意空格)
内容如下:
192.168.1.90 root 123456 22
192.168.1.92 root 123456 22
192.168.1.91 root 123456 22
192.168.1.93 root 123456 22
192.168.1.97 root 123456 22
192.168.1.98 root 123456 22
2.只需要在其中一台服务器执行这个脚本即可 此脚本须与 ip.txt IP配置文件同目录
install.sh 脚本内容:
#!/bin/bash
ip=’’
bip=’’
user=’’
buser=’’
pwd=’’
bpwd=’’
port=’’
bport=’’
logth=install.log
Int(){
#本机密钥对不存在则创建密钥
irsa=~/.ssh/id_rsa
irsap=$irsa’.pub’
if [ ! -f $irsap ];then
echo ‘本机密钥对不存在,正在创建密钥…’ >> ${logth}
ssh-keygen -t rs -P ‘’ -f $irsa
fi
}
Bint(){
#本机公钥复制到外部机器
expect <<EOF
spawn ssh-copy-id -i $irsap -p $port
u
s
e
r
@
user@
user@ip
expect {
“yes/no” { send “yes\n”;exp_continue}
“password” { send "KaTeX parse error: Undefined control sequence: \n at position 4: pwd\̲n̲"} } expe…{ip##.}
bname=‘h’${bip##.}
ssh -p $port
u
s
e
r
@
user@
user@ip “[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -P ‘’ -f /root/.ssh/id_rsa”
echo ‘外部机器密钥对判断不存在则创建密钥…IP=’$bip >> ${logth}
ssh -p $port
u
s
e
r
@
user@
user@ip > /dev/null 2>&1 << eeooff
yum -y install expect
#本机公钥复制到外部机器
expect <<EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -p $bport
b
u
s
e
r
@
buser@
buser@bip
expect {
“yes/no” { send “yes\n”;exp_continue}
“password” { send “$bpwd\n”}
}
expect eof
EOF
hostname $hname
echo $hname > /etc/hostname
echo $bip $bname >> /etc/hosts
expect <<EOF
spawn ssh -p $bport
b
u
s
e
r
@
buser@
buser@bname
expect {
“yes/no” { send “yes\n”}
}
expect eof
EOF
eeooff
echo ‘外部机器公钥复制到本地机器部署…IP=‘
i
p
′
−
−
−
>
>
I
P
=
′
ip'--->>IP='
ip′−−−>>IP=′bip >> KaTeX parse error: Expected 'EOF', got '}' at position 9: {logth} }̲ #获取脚本参数 if [ !…{#iparr[*]}
if [
(
(
((
((count%4)) -ne 0 ];then
echo ‘请确保ip.txt文件内容一个IP对应一个用户名一个密码一个端口 格式为 IP username password port 中间空格隔开’
exit
fi
Int
for(( i=0;i<KaTeX parse error: Expected '}', got '#' at position 2: {#̲iparr[@]};i=i+4))
do
ip=KaTeX parse error: Expected '}', got 'EOF' at end of input: {iparr[i]}
user=KaTeX parse error: Expected '}', got 'EOF' at end of input: {iparr[i+1]}
pwd=KaTeX parse error: Expected '}', got 'EOF' at end of input: {iparr[i+2]}
port=KaTeX parse error: Expected '}', got 'EOF' at end of input: {iparr[i+3]}
#if ping -c 1 KaTeX parse error: Expected 'EOF', got '&' at position 17: …p >/dev/null 2>&̲1;then #echo '本…ip’
Bint
#else
#echo KaTeX parse error: Expected 'EOF', got '#' at position 10: ip'网络不通' #̲fi for (( j={#iparr[@]} ;j>0;j=
j
−
4
)
)
d
o
b
i
p
=
j-4)) do bip=
j−4))dobip={iparr[KaTeX parse error: Expected 'EOF', got '}' at position 5: j-4]}̲ buser={iparr[KaTeX parse error: Expected 'EOF', got '}' at position 5: j-3]}̲ bpwd={iparr[KaTeX parse error: Expected 'EOF', got '}' at position 5: j-2]}̲ bport={iparr[KaTeX parse error: Expected 'EOF', got '}' at position 5: j-1]}̲ if [ "bip" != "
i
p
"
]
;
t
h
e
n
e
c
h
o
′
外
部
机
器
公
钥
复
制
到
本
地
机
器
部
署
.
.
.
I
P
=
′
ip" ];then echo '外部机器公钥复制到本地机器部署...IP='
ip"];thenecho′外部机器公钥复制到本地机器部署...IP=′ip’—>>IP=’$bip
Wint
fi
done
done
echo -e “\033[47;5m 恭喜部署完成.请及时删除文件 ip.txt 以防泄露! \033[0m”