Skip to content
SRE运维进阶之路SRE运维进阶之路
github icon
  • 前端学习笔记

    author iconLinuxStorycalendar icon2021年5月11日category icon
    • Linux
    tag icon
    • Bash
    timer icon小于 1 分钟

    此页内容
    • 开机集成zabbix和jumpserver脚本

    # 开机集成zabbix和jumpserver脚本

    #!/bin/bash
    LOCAL_PASS="****"
    ANS_PASS="****"
    INVENTORY_FILE="/etc/ansible/playbook/zabbix/inventory/zabbix.yml"
    ZABBIX_PALYBOOK_FILE="/etc/ansible/playbook/zabbix/new-zabbix-agent.yml"
    IP=$(ifconfig | grep broadcast | awk '{ print $2 }')
    LAST_TWO_IP=$(echo $IP | cut -d. -f3-4)
    
    node="f6d2228f-c474-4b1e-ac8b-5c7d10e63b53"
    
    update_ssh() {
      sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
      systemctl restart sshd > /dev/null 2>&1
      echo "${LOCAL_PASS}" | passwd root --stdin  > /dev/null 2>&1
    }
    
    integrate_jms() {
      if [[ "${1}" == "csp" ]]; then
        node="b256f30b-896e-44cd-90f0-0fc28984afe9"
      elif [[ "${1}" == "bes" ]]; then
        node="b594957e-d6e8-4c85-92e6-b89757792e1a"
      fi 
      curl -XPOST -H 'Authorization: Token d54c30e98f49de8e4e75bd3c28f214d0dea761ca' \
        -H "Content-Type:application/json" http://192.168.162.10/api/v1/assets/assets/ \
        -d "{ \"ip\": \"${IP}\", \"hostname\": \"${LAST_TWO_IP}-${2}\", 
            \"protocol\": \"ssh\", \"port\": 22, \"protocols\": [ \"ssh/22\" ], 
            \"platform\": \"Linux\", \"is_active\": true, 
            \"admin_user\": \"59bb8ee8-98ad-4aac-a1a7-3e1b2b83f38f\", 
            \"nodes\": [ \"${node}\" ]}"
    }
    
    integrate_ans_zbx() {
      /usr/bin/expect <<EOF
      set timeout 180
      spawn ssh root@192.168.162.1
      expect {
        "(yes/no)?" {
          send "yes\r";
          expect "password:";
          send "${ANS_PASS}\r";
          exp_continue;
        }
        "password:" {
          send "${ANS_PASS}\r";
          exp_continue;
        }
        "*#" {
          send "sed -i '/# ${1} insert/i\  ${IP}:' ${INVENTORY_FILE}\r";
          send "sed -i 's/${IP}/        ${IP}/g' ${INVENTORY_FILE}\r";
          send " ansible-playbook -i ${INVENTORY_FILE} ${ZABBIX_PALYBOOK_FILE} \
                 -l ${IP} --skip-tags 'sangoma,debian,suse,windows,docker'\r";
        }
      }
      expect eof
    EOF
    }
    
    main() {
      update_ssh
      integrate_jms "$@"
      integrate_ans_zbx "$@" 
    }
    
    main "$@"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    edit icon编辑此页open in new window
    上次编辑于: 2022/4/27 15:33:00
    贡献者: clay-wangzhi
    备案号:冀ICP备2021007336号
    Copyright © 2023 LinuxStory