Linux下实现SNMP一键安装的Shell脚本
Net-SNMP是一个免费的、开放源码的SNMP实现,以前称为UCD-SNMP。SNMP很多都用其安装之后,作为监控宝和阿里云的系统信息监控使用。下面就来分享linux下实现SNMP一键安装的shell脚本:
#!/usr/bin/envbash
exportLC_ALL=C
if["$(id-u)"!="0"]
then
echo"Thisscript.mustberunasroot"1>&2
exit1
fi
####checkifgccperlperl-devel(centos)orlibperl-dev(ubuntu)exists,
APT_CMD=`whereisapt-get|awk'{print$2}'`
YUM_CMD=`whereisyum|awk'{print$2}'`
if["$APT_CMD"x!=""x]
then
echo"$APT_CMD"
cmd=`dpkg-l|grepgcc`
if["$cmd"x=""x]
then
apt-getinstall-ygcc
fi
cmd=`dpkg-l|grepperl`
if["$cmd"x=""x]
then
apt-getinstall-yperl
fi
cmd=`dpkg-l|grep"libperl-dev"`
if["$cmd"x=""x]
then
apt-getinstall-ylibperl-dev
fi
elif["$YUM_CMD"x!=""x]
then
echo$YUM_CMD
cmd=`rpm-qgcc|grep-v"notinstalled"`
if["$cmd"x=""x]
then
yuminstallgcc-y1>/dev/null2>&1
fi
cmd=`rpm-qperl|grep-v"notinstalled"`
if["$cmd"x=""x]
then
yuminstall-yperl1>/dev/null2>&1
fi
cmd=`rpm-qperl-devel|grep-v"notinstalled"`
if["$cmd"x=""x]
then
yuminstall-yperl-devel1>/devel/null2>&1
fi
else
echo"yourreleasehavenoyumorapt-get"
fi
functionUsage()
{
echo"用法如下:(注意:2c版本密码必须大于6位,3版本密码必须大于8位)";
echo""
echo"$0-v版本号(2代表2C,3代表版本3)-u用户名-p密码";
echo"$0-v[2|3]-uusername-ppassword";
echo"";
exit0;
}
if[$#-lt1]
then
echo"请输入参数"
Usage
fi
#####getparameters
version=0
username=""
password=""
whilegetopts":v:u:p:"opt;
do
case$optin
v)
version=$OPTARG
if[[${OPTARG:0:1}="-"]]
then
echo""
echo"-vneedargument"
echo""
Usage
fi
;;
u)
username=$OPTARG
if[[${OPTARG:0:1}="-"]]
then
echo""
echo"-uneedargument"
echo""
Usage
fi
;;
p)
password=$OPTARG
if[[${OPTARG:0:1}="-"]]
then
echo""
echo"-pneedargument"
echo""
Usage
fi
;;
:)
Usage
;;
?)
paralist=-1;
Usage
;;
esac
done
###checkversionnumber,mustbe2or3
if[$version-ne"3"-a$version-ne"2"]
then
Usage
exit3
fi
if[$version-eq"3"-a"$username"x=""x]
then
echo"版本3必须要输入用户名"
Usage
fi
###checkpasswordlength,version3bigthen8,version2bigthen6
if["$version"-eq"3"]
then
if[${#password}-lt"8"]
then
echo""
echo"Yourpasswordsmustgreaterthan8bytes"1>&2
echo""
Usage
fi
else###########version=2
if[${#password}-lt"6"]
then
echo""
echo"Yourpasswordsmustgreaterthan6bytes"1>&2
echo""
Usage
fi
fi
wgethttp://download.cloud.360.cn/yjk/net-snmp.tar.gz
tarzxvfnet-snmp.tar.gz
cdnet-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
makeinstall
case$versionin
2)
cat>>/usr/local/snmp/share/snmp/snmpd.conf<<EOF
rocommunity$password101.199.100.150
rocommunity$password220.181.150.98
rocommunity$password180.153.229.230
rocommunity$password220.181.150.125
rocommunity$password103.28.10.223
EOF
;;
3)
cat>>/usr/local/snmp/share/snmp/snmpd.conf<<EOF
rouser$usernameauth
EOF
#echo"createUser$2MD5$3">>/var/net-snmp/snmpd.conf
#cat>>/var/net-snmp/snmpd.conf<<EOF
#createUser$2MD5$3
#EOF
;;
*)
echo"Pleasecheckyourinputversion"1>&2
echo
exit1
;;
esac
i=0
while[$i-lt5]
do
ret=`/usr/local/snmp/sbin/snmpd`
ret=`psaux|grep-vgrep|grepsnmp`
if["$ret"x=""x]
then
((i=i+1))
ret=`/usr/local/snmp/sbin/snmpd`
else
break
fi
done
case$versionin
3)
echo"createUser$usernameMD5$password">>/var/net-snmp/snmpd.conf
;;
esac
pid=`ps-ef|grepsnmpd|grep-vgrep|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$versionin
3)
cmd=`cat/var/net-snmp/snmpd.conf|grep"$username"`
if["$cmd"x==""x]
then
pid=`ps-ef|grepsnmpd|grep-vgrep|awk'{print$2}'`
if["$pid"x!=""x]
then
kill-9$pid
echo"createUser$usernameMD5$password">>/var/net-snmp/snmpd.conf
fi
fi
;;
esac
ret=`ps-ef|grep-vgrep|grepsnmp`
if["$ret"x!=""x]
then
echo"snmpstartsuccess"
echo
else
echo"snmpstartfailed"
echo
exit4
fi
echo"Finish..."
echo
exit0