• 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"
    
  • 相关阅读:
    alpha版本冲刺总结
    近两天项目冲刺
    关于微软必应词典客户端 的案例分析
    第三次作业——结队编程
    hdu 1002 A + B Problem II(大数)
    ZOJ 3805 Machine(二叉树,递归)
    hdu 4704 sum(费马小定理+快速幂)
    欧拉图
    hdu 1116 Play on Words(欧拉通路)
    前50个斐波那契数
  • 原文地址:https://www.cnblogs.com/maomaomaoge/p/15743696.html
Copyright © 2020-2023  润新知