赞
踩
Linux下实现SNMP服务的一键安装配置,具体实现过程如下所示(CentOS 7操作系统)。
(1)角色判断
判断当前登录的用时是否为root用户,防止安装软件时不能进行安装。如果不是root用户将退出脚本执行,否则将继续进行脚本执行。
- #!/bin/bash
-
- #进行一键安装SNMP创建Shell脚本
-
- #
-
- #判断当前用户是否为root
-
- if [ "$(id -u)" != "0" ]
-
- then
-
- echo "This Shell must be run as root" 1>&2
-
- exit 1
-
- fi
检查SNMP服务需要的安装包是否已经安装,如果没有安装通过Shell脚本直接进行安装。
- #检查系统是否存在yum命令
-
- YUM_CMD=`whereis yum | awk '{print $2}'`
-
- if [ "$YUM_CMD" != "" ]
-
- then
-
- #检测是否安装gcc
-
- cmd=`rpm -q gcc | grep -v "not installed"`
-
- if [ "${cmd}x" = "x" ]
-
- then
-
- echo "install gcc start >>>"
-
- yum install -y gcc 1>/dev/null 2>&1
-
- else
-
- echo "not need install gcc"
-
- fi
-
- cmd1=`rpm -q net-snmp | grep -v "not installed"`
-
- if [ "${cmd1}x" = "x" ]
-
- then
-
- echo "install net-snmp start >>>"
-
- yum install -y net-snmp 1>/dev/null 2>&1
-
- else
-
- echo "not need install net-snmp"
-
- fi
-
- cmd2=`rpm -q net-snmp-utils | grep -v "not installed"`
-
- if [ "${cmd2}x" = "x" ]
-
- then
-
- echo "install net-snmp-utils start >>>"
-
- yum install -y net-snmp-utils 1>/dev/null 2>&1
-
- else
-
- echo "not need install net-snmp-utils"
-
- fi
-
- else
-
- echo "Don't have yum"
-
- exit 1
-
- fi
(3)修改配置文件
修改SNMP服务的配置文件,自定义SNMP的共同体名称以及SNMP结果视图节点,具体如下所示。
- SNMPConfName="/etc/snmp/snmpd.conf"
-
- if [ -f ${SNMPConfName} ]
-
- then
-
- #配置SNMP服务,将原始的SNMP配置文件进行重命名
-
- echo "config file start >>>"
-
- mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak
-
- #创建配置文件赋予权限
-
- touch ${SNMPConfName}
-
- chmod 777 ${SNMPConfName}
-
- echo "##" >> ${SNMPConfName}
-
- #配置SNMP配置文件内容
-
- echo "com2sec mynetwork default public" >> ${SNMPConfName}
-
- echo "group MyROGroup v1 mynetwork" >> ${SNMPConfName}
-
- echo "group MyROGroup v2c mynetwork" >> ${SNMPConfName}
-
- echo "group MyROGroup usm mynetwork" >> ${SNMPConfName}
-
- echo "view systemview included .1.3.6.1.2.1.1" >> ${SNMPConfName}
-
- echo "view systemview included .1.3.6.1.2.1.25.1.1" >> ${SNMPConfName}
-
- echo "view all included .1" >> ${SNMPConfName}
-
- echo "access MyROGroup "" any noauth 0 all all none" >> ${SNMPConfName}
-
- echo "dontLogTCPWrappersConnects yes" >> ${SNMPConfName}
-
- echo "agentaddress 161" >> ${SNMPConfName}
-
- else
-
- echo "Config File not exit"
-
- fi
(4)服务与安全配置
启动SNMP服务,并设置SNMP自启动;配置防火墙允许161/udp端口进行访问,具体配置如下。
- #服务管理与安全配置
-
- if [ -f ${SNMPConfName} ]
-
- then
-
- #服务启动
-
- echo "Service snmpd start"
-
- systemctl restart snmpd
-
- systemctl enable snmpd
-
-
-
- #防火墙配置
-
- echo "firewall config start"
-
- firewall-cmd --zone=public --add-port=161/udp --permanent
-
- firewall-cmd --reload
-
- fi
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。