脚本分享如下:

#!/bin/bash

# Check if the script is running as root
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root"
  exit 1
fi

# Detect the operating system
if [ -f /etc/centos-release ]; then
  os_version=$(cat /etc/centos-release | awk '{print $4}' | awk -F . '{print $1}')
elif [ -f /etc/lsb-release ]; then
  os_version=$(lsb_release -r | awk '{print $2}' | awk -F . '{print $1}')
else
  echo "Unsupported operating system"
  exit 1
fi

# Install Zabbix agent
if [ "$os_version" == "7" ]; then
  yum install https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
  yum install zabbix-agent
elif [ "$os_version" == "6" ]; then
  yum install https://repo.zabbix.com/zabbix/5.0/rhel/6/x86_64/zabbix-release-5.0-1.el6.noarch.rpm
  yum install zabbix-agent
elif [ "$os_version" == "20" ]; then
  wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb
  dpkg -i zabbix-release_5.0-1+focal_all.deb
  apt update
  apt install zabbix-agent
else
  echo "Unsupported operating system version"
  exit 1
fi

# Configure Zabbix agent
read -p "Please enter the Zabbix server IP address: " server_ip
sed -i "s/Server=127.0.0.1/Server=$server_ip/g" /etc/zabbix/zabbix_agentd.conf

# Open firewall for Zabbix agent
if [ "$os_version" == "7" ]; then
  firewall-cmd --permanent --add-port=10050/tcp
  firewall-cmd --reload
elif [ "$os_version" == "6" ]; then
  iptables -I INPUT -p tcp --dport 10050 -j ACCEPT
  service iptables save
elif [ "$os_version" == "20" ]; then
  ufw allow 10050/tcp
else
  echo "Unsupported operating system version"
  exit 1
fi

# Restart Zabbix agent
systemctl restart zabbix-agent