感谢李大神的帮助~


啰嗦:系统大批量上线时,我们一个个去装agent,有点枯燥,这里分享个expect结合上一篇《zabbix自动发现》,可以省不少工时。


准备工作:

    1、下载客户端去吧https://www.zabbix.com/download_agents

    2、跑脚本的主机192.168.2.100

         1)下载的客户端放在了/opt下面

         2)需要安装agent的(ip)和(passwd)信息,在文件ip_list.sh中

            # vim /opt/ip_list.sh

              192.168.2.1:123456

              192.168.2.2:123456

              192.168.2.3:123456

              192.168.2.4:123456

              192.168.2.5:123456

         3)脚本agent_install.sh也放在了/opt下面

    3、注意修改Zabbix Server IP、跑脚本的IP(我标红了)


环境信息:

    1、跑脚本的IP:192.168.2.100

    2、Zabbix Server IP:192.168.2.201

    3、agent安装路径:/usr/local/zabbix

       agent日志:/tmp下面

    4、设置了开启启动



#!/bin/bash


# User:zgd

# https://www.zabbix.com/download_agents

# Install_URL:/usr/local/zabbix

# DATE:20180622

# Version:V2.3


rm -rf /root/.ssh/known_hosts

for i in {1..5}

do

 host=`sed -n "${i}p" /opt/ip_list.txt |awk -F ":" '{print $1}'`

 passwd=`sed -n "${i}p" /opt/ip_list.txt |awk -F ":" '{print $2}'`

  expect << EOF

  set timeout 300

  spawn ssh root@$host

  expect "yes/no" { send "yes\r" }

  expect "password" { send "${passwd}\r" }

  expect "#" { send "groupadd zabbix\r" }

  expect "#" { send "useradd -g zabbix zabbix -s /sbin/nologin\r" }

  expect "#" { send "mkdir /usr/local/zabbix\r" }

  expect "#" { send "scp 192.168.2.100:/opt/zabbix_agents_3.2.0.linux2_6.amd64.tar.gz /usr/local/zabbix\r" }

  expect "yes/no" { send "yes\r" }

  expect "password" { send "123456\r" }

  expect "#" { send  "cd /usr/local/zabbix\r" }

  expect "#" { send "tar xf zabbix_agents_3.2.0.linux2_6.amd64.tar.gz\r" }

  expect "#" { send "sed -i '91c Server=192.168.2.201' /usr/local/zabbix/conf/zabbix_agentd.conf\r" }

  expect "#" { send "sed -ri \"/^Hostname/s/.*/Hostname=\\\${HOSTNAME%%.*}/\" /usr/local/zabbix/conf/zabbix_agentd.conf\r" }

  expect "#" { send "/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf\r" }

  expect "#" { send "chmod -R 755 /usr/local/zabbix\r" }

  expect "#" { send "chown -R zabbix. /usr/local/zabbix\r" }

  expect "#" { send "rm -rf /usr/local/zabbix/zabbix_agents_3.2.0.linux2_6.amd64.tar.gz\r" }

  expect "#" { send "echo \"/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf\" >> /etc/rc.local\r" }

  expect "#" { send "exit\r" }

EOF

done