Linux下实现SNMP一键安装的Shell脚本

所属分类: 脚本专栏 / linux shell 阅读数: 313
收藏 0 赞 0 分享

Net-SNMP是一个免费的、开放源码的SNMP实现,以前称为UCD-SNMP。SNMP 很多都用其安装之后,作为监控宝和阿里云的系统信息监控使用。下面就来分享linux下实现SNMP一键安装的shell脚本:

#!/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 "用法如下:(注意:2c版本密码必须大于6位,3版本密码必须大于8位)";
    echo ""
    echo "  $0 -v 版本号(2代表2C,3代表版本3) -u 用户名 -p 密码";
    echo "  $0 -v [2|3] -u username -p password";
    echo "";
    exit 0;
 }
 if [ $# -lt 1 ]
 then
   echo "请输入参数"
   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 "版本3必须要输入用户名"
   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>&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
更多精彩内容其他人还在看

Apache使用 .htaccess 来实现强制https访问的方法

下面小编就为大家带来一篇Apache使用 .htaccess 来实现强制https访问的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Linux shell 之 提取文件名和目录名的一些方法总结

本篇文章主要介绍了Linux shell 之 提取文件名和目录名的一些方法总结,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

详解shell 遍历文件夹内所有文件并打印绝对路径

本篇文章主要介绍了shell 遍历文件夹内所有文件并打印绝对路径,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

在linux的终端退出python命令行的方法

下面小编就为大家带来一篇在linux的终端退出python命令行的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Log4j 日志文件Linux/Mac/Windows通用存放位置设置方法

下面小编就为大家带来一篇Log4j 日志文件Linux/Mac/Windows通用存放位置设置方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Linux shell命令帮助格式详解

最近看了一个教程,关于Linux命令的,本来以为当是复习随便看看的,结果看了不禁汗颜,这个真挺有学问的,很多东西都是我还不知道的,故此做总结。下面这篇文章主要介绍了Linux shell命令帮助格式的相关资料,需要的朋友可以参考借鉴。
收藏 0 赞 0 分享

linux shell发送Email邮件的方法详解

这篇文章主要介绍了linux shell发送Email邮件的方法,文中介绍的内容包括发送一封简单的邮件、邮件的格式、邮件标题使用中文以及邮件内容使用html等相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

详解Linux命令中的正则表达式

正则表达式是一套由多个元字符组成的模糊查找模式,使用正则表达式可以快速查找和定位文本中指定的内容。接下来通过本文给大家介绍Linux命令中的正则表达式,需要的朋友参考下吧
收藏 0 赞 0 分享

linux下安装配置Memcache和PHP环境的实现

下面小编就为大家带来一篇linux下安装配置Memcache和PHP环境的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

php在linux中可能用到的命令(推荐)

下面小编就为大家带来一篇php在linux中可能用到的命令(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多