#!/bin/bash
#date 2022/8/8
#mail it-arch
#############
echo "##########################################"
echo "Auto Install Linux-snmp.                ##"
echo "Press Ctrl + C to cancel                ##"
echo "Any key to continue                     ##"
echo "##########################################"
read -n 1
SNMPFILE=/etc/snmp/snmpd.conf
read -p "Please input your community:" COMMUNITY
yum install net-snmp net-snmp-devel net-snmp-utils -y
sleep 3
if [ -f "$SNMPFILE" ]
	then
	cp $SNMPFILE /etc/snmp/snmpd.conf.bak
fi
cat > $SNMPFILE << EOF
com2sec notConfigUser  default       $COMMUNITY
group   notConfigGroup v1           notConfigUser
group   notConfigGroup v2c           notConfigUser
view    systemview    included   .1
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
access  notConfigGroup ""      any       noauth    exact  all  none none
view all    included  .1                               80
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes
proc mountd
proc ntalkd 4
proc senmail 10 1
exec echotest /bin/echo hello world
disk / 10000
EOF
/etc/init.d/iptables status >/dev/null 2>&1
if [ $? -eq 0 ]
then
iptables -I INPUT -p udp --dport 161 -j ACCEPT && 
iptables-save >/dev/null 2>&1
else
	echo -e "\033[32m iptables is stopd\033[0m"
fi

################################################################
IP=`ifconfig |grep "inet addr"|grep -v 127.0.0.1|cut -d: -f2|cut -d' ' -f1`
SNMPPORT=`netstat -lntup | grep 161 | wc -l`
SNMPPROCESS=`ps -ef | grep snmp | grep -v grep | wc -l`
if [ $SNMPPORT == $SNMPPROCESS ]
	then
		/etc/init.d/snmpd start
else
		/etc/init.d/snmpd restart
fi
chkconfig snmpd on
echo -e "\033[32m Snmp is install success,ipaddress is:$IP,community is:$COMMUNITY\033[0m"
