zoukankan      html  css  js  c++  java
  • 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"
    
  • 相关阅读:
    Two strings CodeForces
    Dasha and Photos CodeForces
    Largest Beautiful Number CodeForces
    Timetable CodeForces
    Financiers Game CodeForces
    AC日记——整理药名 openjudge 1.7 15
    AC日记——大小写字母互换 openjudge 1.7 14
    AC日记——将字符串中的小写字母换成大写字母 openjudge 1.7 13
    AC日记——加密的病历单 openjudge 1.7 12
    AC日记——潜伏着 openjudge 1.7 11
  • 原文地址:https://www.cnblogs.com/maomaomaoge/p/15743696.html
Copyright © 2011-2022 走看看