snmpd/install.sh

116 lines
3.6 KiB
Bash
Raw Permalink Normal View History

2020-07-08 10:04:08 -04:00
# SNMPD install script
2020-07-08 09:21:41 -04:00
2020-07-08 10:04:08 -04:00
OSID=$(awk -F= '/^ID=/{print $2}' /etc/os-release | sed 's/"//g')
2020-07-08 09:21:41 -04:00
2020-07-08 10:04:08 -04:00
### SNMPWALK HELP ####
# Usefull for testing
#snmpwalk -v2c -c rouser987 ip_hostname
#snmpwalk -v3 -l authPriv -u rouser987 -a SHA -A "pass1" -x AES -X "pass2" ip_hostname
if [[ OSID == "centos" ]]; then
# IF USING CENTOS
yum install -y net-snmp
systemctl start snmpd
elif [[ OSID == "debian" ]]; then
# IF USING DEBIAN
apt install -y snmpd
fi
# Stop snmpd so we can do our config
2020-07-08 09:21:41 -04:00
systemctl stop snmpd
2020-07-08 10:04:08 -04:00
# enable it so it will work on startup
2020-07-08 09:21:41 -04:00
systemctl enable snmpd
2020-07-08 10:04:08 -04:00
# Make config changes
2020-07-08 09:21:41 -04:00
cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.orig
echo "" > /etc/snmp/snmpd.conf
curl -o /etc/snmp/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
chmod +x /etc/snmp/distro
nano /etc/snmp/snmpd.conf
2020-07-08 10:04:08 -04:00
cat <<EOF > /etc/snmp/snmpd.conf
2020-07-08 09:21:41 -04:00
# IF SERVER REMOTE #
agentaddress udp:16161
rouser rouser987
syslocation VMENV | LOCATION
syscontact EMAIL
2020-07-08 10:04:08 -04:00
#sysname CHANGENAMEHERE
2020-07-08 09:21:41 -04:00
#Distro Detection
extend .1.3.6.1.4.1.2021.7890.1 distro /etc/snmp/distro
#Hardware Detection (uncomment to enable)
extend .1.3.6.1.4.1.2021.7890.2 hardware '/bin/cat /sys/devices/virtual/dmi/id/product_name'
extend .1.3.6.1.4.1.2021.7890.3 manufacturer '/bin/cat /sys/devices/virtual/dmi/id/sys_vendor'
#extend .1.3.6.1.4.1.2021.7890.4 serial '/bin/cat /sys/devices/virtual/dmi/id/product_serial'
# END REMOTE
2020-07-08 10:04:08 -04:00
EOF
2020-07-08 09:21:41 -04:00
2020-07-08 10:04:08 -04:00
cat <<EOF > /etc/snmp/snmpd.conf
2020-07-08 09:21:41 -04:00
# IF SERVER LOCAL #
agentaddress udp:161
rouser rouser987
syslocation VMENV | LOCATION
syscontact EMAIL
2020-07-08 10:04:08 -04:00
#sysname CHANGENAMEHERE
2020-07-08 09:21:41 -04:00
#Distro Detection
extend .1.3.6.1.4.1.2021.7890.1 distro /etc/snmp/distro
#Hardware Detection (uncomment to enable)
extend .1.3.6.1.4.1.2021.7890.2 hardware '/bin/cat /sys/devices/virtual/dmi/id/product_name'
extend .1.3.6.1.4.1.2021.7890.3 manufacturer '/bin/cat /sys/devices/virtual/dmi/id/sys_vendor'
#extend .1.3.6.1.4.1.2021.7890.4 serial '/bin/cat /sys/devices/virtual/dmi/id/product_serial'
# END LOCAL
2020-07-08 10:04:08 -04:00
OEF
2020-07-08 09:21:41 -04:00
2020-07-08 10:04:08 -04:00
if [[ "${OSID}" == "centos" ]]; then
# IF USING CENTOS This creates a random password1 and password 2. I like to log the output somewhere that way I have the info if I need it later #
echo "createUser rouser987 SHA \""$(tr -cd '[:alnum:]' < /dev/urandom | fold -w50 | head -n1)"\" AES \""$(tr -cd '[:alnum:]' < /dev/urandom | fold -w50 | head -n1)"\"" | tee -a /var/lib/net-snmp/snmpd.conf
elif [[ "${OSID}" == "debian" ]]; then
# IF USING DEBIAN This creates a random password1 and password 2. I like to log the output somewhere that way I have the info if I need it later #
echo "createUser rouser987 SHA \""$(tr -cd '[:alnum:]' < /dev/urandom | fold -w50 | head -n1)"\" AES \""$(tr -cd '[:alnum:]' < /dev/urandom | fold -w50 | head -n1)"\"" | tee -a /var/lib/snmp/snmpd.conf
if
2020-07-08 09:21:41 -04:00
systemctl start snmpd
#### IF THERE'S FIREWALL-CMD ####
# IF IT'S A LOCAL SERVER #
2020-07-08 10:04:08 -04:00
nano /etc/firewalld/services/snmp.xml
2020-07-08 09:21:41 -04:00
# Paste the below in the file
2020-07-08 10:04:08 -04:00
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SNMP</short>
<description>SNMP protocol</description>
<port protocol="udp" port="161"/>
</service>
2020-07-08 09:21:41 -04:00
firewall-cmd --reload
firewall-cmd --zone=public --add-service snmp --permanent
# IF IT'S A REMOTE SERVER #
firewall-cmd --permanent --zone=public --add-rich-rule='
rule family="ipv4"
source address="LOCALIP"
port protocol="udp" port="16161" accept'
# ON LOCAL AND REMOTE #
firewall-cmd --reload
#### IF THERE'S IPTABLES ####
# IF IT'S LOCAL
iptables -A INPUT -p udp --dport 161 -j ACCEPT
# Then save the rules however you would do it
# IF IT'S REMOTE
iptables -A INPUT -p udp -s LOCALIP --dport 16161 -j ACCEPT
2020-07-08 10:04:08 -04:00
# Then save the rules however you would do it