Linux to implement SNMP one-click Install shell script _linux Shell

Source: Internet
Author: User
Tags md5 snmp


Net-snmp is a free, open source SNMP implementation, formerly known as UCD-SNMP. Many SNMP are used after its installation, as a monitoring treasure and Aliyun system information monitoring. Let's share the Linux shell script that implements SNMP one-click installation:


#!/usr/bin/env bash
export LC_ALL=C
if [ "$(id -u)" != "0" ]
Then
echo "This script. must be run as root" 1>&2
Exit 1
Fi
####check if gcc perl perl-devel(centos) or libperl-dev(ubuntu) exists,
APT_CMD=`whereis apt-get|awk '{print $2}'`
YUM_CMD=`whereis yum|awk '{print $2}'`
if [ "$APT_CMD"x != ""x ]
Then
echo "$APT_CMD"
cmd=`dpkg -l|grep gcc`
if [ "$cmd"x = ""x ]
Then
apt-get install -y gcc 
Fi
cmd=`dpkg -l|grep perl`
if [ "$cmd"x = ""x ]
Then
apt-get install -y perl 
Fi
cmd=`dpkg -l|grep "libperl-dev"`
if [ "$cmd"x = ""x ]
Then
apt-get install -y libperl-dev 
Fi
elif [ "$YUM_CMD"x != ""x ]
Then
echo $YUM_CMD
cmd=`rpm -q gcc|grep -v "not installed"`
if [ "$cmd"x = ""x ]
Then
yum install gcc -y 1>/dev/null 2>&1
Fi
cmd=`rpm -q perl|grep -v "not installed"`
if [ "$cmd"x = ""x ]
Then
yum install -y perl 1>/dev/null 2>&1
Fi
cmd=`rpm -q perl-devel|grep -v "not installed"`
if [ "$cmd"x = ""x ]
Then
yum install -y perl-devel 1>/devel/null 2>&1
Fi
Else
echo "your release have no yum or apt-get"
Fi
function Usage()
{
Echo "is used as follows: (Note: the password of version 2C must be greater than 6 bits, and that of version 3 must be greater than 8 bits)";
Echo ""
Echo "$0 - V version number (2 for 2c, 3 for version 3) - U user name - P password";
echo "  $0 -v [2|3] -u username -p password";
Echo "";
Exit 0;
}
if [ $# -lt 1 ]
Then
Echo "please enter parameters"
Usage
Fi
#####get parameters
Version=0
Username= ""
Password= ""
while getopts ":v:u:p:" opt;
Do
Case $opt in
V)
version=$OPTARG
if [[ ${OPTARG:0:1} = "-" ]]
Then
Echo ""
echo "-v need argument"
Echo ""
Usage
Fi
;
U)
username=$OPTARG
if [[ ${OPTARG:0:1} = "-" ]]
Then
Echo ""
echo "-u need argument"
Echo ""
Usage
Fi
;
P)
password=$OPTARG
if [[ ${OPTARG:0:1} = "-" ]]
Then
Echo ""
echo "-p need argument"
Echo ""
Usage
Fi
;
)
Usage
;
?)
Paralist=-1;
Usage
;
ESAC
Done
###check version number, must be 2 or 3
if [ $version -ne "3" -a $version -ne "2" ]
Then
Usage
Exit 3
Fi
if [ $version -eq "3" -a "$username"x = ""x ]
Then
Echo "version 3 must enter user name"
Usage
Fi
###check password length, version 3 big then 8, version 2 big then 6
if [ "$version" -eq "3" ] 
Then
if [ ${#password} -lt "8" ]
Then
Echo ""
echo "Your passwords must greater than 8 bytes" 1>&2
Echo ""
Usage
Fi
else     ########### version = 2
if [ ${#password} -lt "6" ]
Then
Echo ""
echo "Your passwords must greater than 6 bytes" 1>&2
Echo ""
Usage
Fi
Fi
wget http://download.cloud.360.cn/yjk/net-snmp.tar.gz
tar zxvf net-snmp.tar.gz
cd net-snmp-5.7.2
./configure --prefix=/usr/local/snmp --with-mib-modules=ucd-snmp/diskio 
-enable-mfd-rewrites --with-default-snmp-version="$version" --with-sys-contact="@@no.where"
--with-sys-location="Unknown" --with-logfile="/var/log/snmpd.log"
--with-persistent-directory="/var/net-snmp"
Make
Make install
case $version in
2)
cat >> /usr/local/snmp/share/snmp/snmpd.conf << EOF
rocommunity $password 101.199.100.150
rocommunity $password 220.181.150.98
rocommunity $password 180.153.229.230
rocommunity $password 220.181.150.125
rocommunity $password 103.28.10.223
EOF
;
3)
cat >> /usr/local/snmp/share/snmp/snmpd.conf << EOF
rouser $username auth
EOF
#   echo "createUser $2 MD5 $3" >>/var/net-snmp/snmpd.conf
#   cat >> /var/net-snmp/snmpd.conf << EOF
#createUser $2 MD5 $3
#EOF
;
*)
echo "Please check your input version" 1>&amp;2
Echo
Exit 1
;
ESAC
I=0
while [ $i -lt 5 ]
Do
ret=`/usr/local/snmp/sbin/snmpd`
ret=`ps aux | grep -v grep | grep snmp`
if [ "$ret"x = ""x ]
Then
((i=i+1))
ret=`/usr/local/snmp/sbin/snmpd`
Else
Break
Fi
Done
case $version in
3)
echo "createUser $username MD5 $password" >>/var/net-snmp/snmpd.conf
;
ESAC
pid=`ps -ef|grep snmpd|grep -v grep|awk '{print $2}'`
echo "pid: $pid"
if [ "$pid"x != ""x ]
Then
Kill -9 $pid
/usr/local/snmp/sbin/snmpd
Else
/usr/local/snmp/sbin/snmpd
Fi
case $version in
3)
cmd=`cat /var/net-snmp/snmpd.conf|grep "$username"`
if [ "$cmd"x == ""x ]
Then
pid=`ps -ef|grep snmpd|grep -v grep|awk '{print $2}'`
if [ "$pid"x != ""x ]
Then
Kill -9 $pid
echo "createUser $username MD5 $password" >>/var/net-snmp/snmpd.conf
Fi
Fi
;
ESAC
ret=`ps -ef | grep -v grep | grep snmp`
if [ "$ret"x != ""x ]
Then
echo "snmp start success"
Echo
Else
echo "snmp start failed"
Echo
Exit 4
Fi
echo "Finish..."
Echo
Exit 0 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.