Advertisement
Guest User

net-snmp-create-v3-user

a guest
Oct 16th, 2014
1,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.81 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # this shell script is designed to add new SNMPv3 users
  6. # to Net-SNMP config file.
  7.  
  8. prefix=/usr
  9. exec_prefix=/usr
  10. includedir=/usr/include
  11. libdir=/usr/lib64
  12. datarootdir=${prefix}/share
  13. NSC_LDFLAGS="-Wl,-z,relro -Wl,-z,now"
  14. NSC_INCLUDEDIR=${includedir}
  15. NSC_LIBDIR=-L${libdir}
  16. NSC_LIBS=""
  17. NSC_AGENTLIBS=" -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE"
  18. NSC_PREFIX=$prefix
  19. NSC_EXEC_PREFIX=$exec_prefix
  20. NSC_SRCDIR=.
  21. NSC_INCDIR=${NSC_PREFIX}/include
  22. NSC_BASE_SUBAGENT_LIBS="-lnetsnmpagent -lnetsnmphelpers -lnetsnmp"
  23. NSC_BASE_AGENT_LIBS="-lnetsnmpagent -lnetsnmphelpers -lnetsnmpmibs -lnetsnmp"
  24. NSC_SRC_LIBDIRS="agent/.libs snmplib/.libs agent/helpers/.libs"
  25. NSC_SRC_LIBDEPS="agent/.libs/libnetsnmpmibs.a agent/.libs/libnetsnmpagent.a agent/helpers/.libs/libnetsnmphelpers.a snmplib/.libs/libnetsnmp.a"
  26.  
  27. if test "x$NSC_SRCDIR" = "x." ; then
  28.    NSC_SRCDIR="NET-SNMP-SOURCE-DIR"
  29. fi
  30.  
  31. if /bin/ps -e | egrep ' snmpd *$' > /dev/null 2>&1 ; then
  32.     echo "Apparently at least one snmpd demon is already running."
  33.     echo "You must stop them in order to use this command."
  34.     exit 1
  35. fi
  36.  
  37. Aalgorithm="MD5"
  38. Xalgorithm="DES"
  39. token=rwuser
  40.  
  41. while test "x$done" = "x" -a "x$1" != "x" -a "x$usage" != "xyes"; do
  42. case "$1" in
  43.     -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  44.     *) optarg= ;;
  45. esac
  46.  
  47. unset shifted
  48. case $1 in
  49.     --version|--ver*)
  50.       echo 5.5
  51.       ;;
  52.     --help)
  53.       usage="yes"
  54.       ;;
  55.  
  56.     -A|-a)
  57.     shift
  58.     if test "x$1" = "x" ; then
  59.         echo "You must specify an authentication algorithm or pass phrase"
  60.         exit 1
  61.     fi
  62.         case $1 in
  63.             MD5|SHA)
  64.         Aalgorithm=$1
  65.         shift
  66.         ;;
  67.             md5|sha)
  68.         Aalgorithm=`echo $1 | tr a-z A-Z`
  69.         shift
  70.         ;;
  71.             *)
  72.         apassphrase=$1
  73.         shift
  74.         ;;
  75.         esac
  76.         ;;
  77.     -X|-x)
  78.     shift
  79.     if test "x$1" = "x" ; then
  80.         echo "You must specify an encryption algorithm or pass phrase"
  81.         exit 1
  82.     fi
  83.         case $1 in
  84.             DES|AES|AES128)
  85.         Xalgorithm=$1
  86.         shift
  87.         ;;
  88.             des|aes|aes128)
  89.         Xalgorithm=`echo $1 | tr a-z A-Z`
  90.         shift
  91.         ;;
  92.             *)
  93.         xpassphrase=$1
  94.         shift
  95.         ;;
  96.     esac
  97.     ;;
  98.     -ro)
  99.         token="rouser"
  100.     shift
  101.     ;;
  102.     -*)
  103.     echo "unknown suboption to $0: $1"
  104.     usage=yes
  105.     done=1
  106.     ;;
  107.     *)
  108.         done=1
  109.         ;;
  110.     esac
  111. done
  112.  
  113. if test "x$usage" = "xyes"; then
  114.     echo ""
  115.     echo "Usage:"
  116.     echo "  net-snmp-create-v3-user [-ro] [-A authpass] [-X privpass]"
  117.     echo "                          [-a MD5|SHA] [-x DES|AES] [username]"
  118.     echo ""
  119.     exit
  120. fi
  121.  
  122. if test "x$1" = "x" ; then
  123.     prompt=yes
  124.     echo "Enter a SNMPv3 user name to create: "
  125.     read user
  126. else
  127.     user=$1
  128.     shift
  129. fi
  130. if test "x$user" = "x" ; then
  131.     echo "You must specify a user name"
  132.     exit 1
  133. fi
  134. if test "x$apassphrase" = "x" ; then
  135.     prompt=yes
  136.     echo "Enter authentication pass-phrase: "
  137.     read apassphrase
  138. fi
  139. if test "x$apassphrase" = "x" ; then
  140.     echo "You must specify an authentication pass-phrase"
  141.     exit 1
  142. fi
  143.     if test "x$prompt" = "xyes" -a "x$xpassphrase" = "x" ; then
  144.     echo "Enter encryption pass-phrase: "
  145.     echo "  [press return to reuse the authentication pass-phrase]"
  146.     read xpassphrase
  147. fi
  148. outdir="/var/lib/snmp"
  149. outfile="$outdir/snmpd.conf"
  150. line="createUser $user $Aalgorithm \"$apassphrase\" $Xalgorithm $xpassphrase"
  151. echo "adding the following line to $outfile:"
  152. echo "  " $line
  153. # in case it hasn't ever been started yet, start it.
  154. if test ! -d $outdir ; then
  155.     mkdir $outdir
  156. fi
  157. if test ! -d $outfile ; then
  158.     touch $outfile
  159. fi
  160. echo $line >> $outfile
  161. outfile="/etc/snmp/snmpd.conf"
  162. line="$token $user"
  163. echo "adding the following line to $outfile:"
  164. echo "  " $line
  165. if test ! -d $outfile ; then
  166.     touch $outfile
  167. fi
  168. echo $line >> $outfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement