Linux下实现SNMP一键安装的Shell脚本
程序员文章站
2023-11-18 16:00:04
net-snmp是一个免费的、开放源码的snmp实现,以前称为ucd-snmp。snmp 很多都用其安装之后,作为监控宝和阿里云的系统信息监控使用。下面就来分享linux下...
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
推荐阅读
-
Linux下实现SNMP一键安装的Shell脚本
-
Linux下实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本分享
-
linux下采用shell脚本实现批量为指定文件夹下图片添加水印的方法
-
让Linux下的cron以秒为单位执行shell脚本的3种方法
-
Linux下查找后门程序 CentOS 查后门程序的shell脚本
-
linux和windows下的自动ftp脚本(shell bat)
-
Oracle VM VirtualBox 在linux系统下安装增强插件实现访问主机的共享文档方法
-
Linux下如何实现shell多线程编程以提高应用程序的响应
-
使用Python编写一个在Linux下实现截图分享的脚本的教程
-
linux下实现ftp自动备份shell脚本