jenkins_ssh

实现shell结构体数组

实现expect和ssh, scp

#!/bin/bash

# 参考: https://stackoverflow.com/questions/15684646/array-like-data-structure-in-bash-config-file
# 参考: https://blog.csdn.net/yuesichiu/article/details/17245089

declare -A box2032=([ip]=192.168.20.32 [port]=22 [pwd]=aaa [posidas]=/opt/dass/dasserver)
declare -A box2028=([ip]=192.168.20.28 [port]=22 [pwd]=aa [posidas]=/opt/dass/dasserver)
declare -A box2048=([ip]=192.168.20.48 [port]=22 [pwd]=aaa [posidas]=/root/dasserver)
declare -A box50135=([ip]=192.168.50.135 [port]=2233 [pwd]=aaa [posidas]=/root/dasserver)

declare -a boxs=(box2032 box2028 box2048 box50135)

cd ..

for ((i=0; i<${#boxs[@]}; i++))
do
{
box=${boxs[$i]}
ip=$box[ip]
port=$box[port]
pwd=$box[pwd]
posi=$box[posidas]

# 发送命令删除老旧文件命令
/usr/bin/expect <<-EOF

set timeout -1
spawn ssh -p ${!port} root@${!ip}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "${!pwd}\r" }
}

expect "#*"
send "rm -rf /home/mhx\r"
send "mkdir -p /home/mhx\r"
send "exit\r"

expect eof
EOF


# 发送文件
/usr/bin/expect <<-EOF

set timeout -1
# 注意下面是大写的P
spawn scp -P ${!port} ./dasserver root@${!ip}:/home/mhx
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "${!pwd}\r" }
}

expect eof
EOF

# 发送命令,替换可执行程序
/usr/bin/expect <<-EOF

set timeout -1
spawn ssh -p ${!port} root@${!ip}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "${!pwd}\r" }
}


expect "#*"
send "rm -rf ${!posi}/dasserver\r"
expect "#"
send "cp -r  /home/mhx/dasserver ${!posi}/dasserver\r"
send "chmod 777 ${!posi}/dasserver\r"
send "pkill dasserver\r"
send "exit\r"

expect eof
EOF

} &
done

wait 

echo "大盒子全部ok"
原文地址:https://www.cnblogs.com/maomaomaoge/p/15743696.html