感谢李大神的帮助~
废话: 系统大批量上线时, 我们一个个去装 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
来源: http://www.bubuko.com/infodetail-2655564.html