跳板机管理平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.8 KiB

5 years ago
  1. #!/bin/bash
  2. # -------------------------------------
  3. # 用户管理
  4. #
  5. # @author golanstone
  6. # @date 2020-03-25
  7. # -------------------------------------
  8. cmdOp=$1 # 操作码 add del sudo unsudo
  9. cmdName=$2 # 用户名
  10. cmdPassword=$3 # 密码
  11. cmdAuthorizedKeys=`echo ${@:4}` # ssh公钥
  12. # cmdAuthorizedKeys="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDiLQnYvqNnDcsR6lvrUL3SgmyPJ3XqGork2IxZMPyu+68dZC2/DIaVYm2G0NeEdnDlExkmIrhzRWpfmz6H748TFFsTvGxeOOR+djjKWwOMwmxU0y8QDseZqEAuCANTjzBjGu7/RUWQ5ysOKD8+UTdr1W+avumfFbBzFXNHSHA3JBFUFvFOWxcggBAlOBbA3fWig6a/ykepBfimEdgcyq/P7ERRsP5eLxasmf/vUV3vVE04SpkpMXniG8r9z3gP7At/TFWnvCWmmBJ9+EUK6FE7fxV4tmEni+IfkpQog+l5SpOp2XpMHp8YdIgotwdAKoOu3/bRsqeEMMNYErV+WsFF Stone@Golanstone"
  13. # 操作码
  14. OpAdd=1 # 添加用户
  15. OpDel=2 # 删除用户
  16. OpSudo=3 # 给用户加sudo权限
  17. SUDOFILE=/etc/sudoers # 系统sudoer文件
  18. SYSPASSWD=/etc/passwd # 系统passwd文件
  19. USER_HOME_DIR=/home/$cmdName # 家目录
  20. USER_AUTHORIZED_KEYS=/home/$cmdName/.ssh/authorized_keys # ssh免密认证文件
  21. USER_SSH_DIR=/home/$cmdName/.ssh # ssh目录
  22. USER_SSH_RSA_PRIVATE=/home/$cmdName/.ssh/id_rsa # ssh私钥
  23. # 检查cmd参数
  24. check() {
  25. echo $cmdOp $cmdName $cmdPassword $cmdAuthorizedKeys
  26. }
  27. # 添加用户
  28. add_user() {
  29. # 判断用户是否存在
  30. grep "^$cmdName" $SYSPASSWD >& /dev/null
  31. if [ $? -ne 0 ]; then
  32. # 创建用户并设置密码
  33. useradd $cmdName
  34. echo "password" $cmdPassword
  35. echo $cmdPassword| passwd $cmdName --stdin &>/dev/null
  36. if [ $? -eq 0 ];then
  37. echo "${cmdName}'s password is set successful"
  38. else
  39. echo "${cmdName}'s password is set failed"
  40. fi
  41. fi
  42. # 生成.ssh目录
  43. if [ ! -d $USER_SSH_DIR ]; then
  44. sudo -u $cmdName mkdir $USER_SSH_DIR && chmod 700 $USER_SSH_DIR
  45. echo "mkdir $USER_SSH_DIR"
  46. fi
  47. # 生成authorized_keys文件
  48. if [ ! -f $USER_AUTHORIZED_KEYS ]; then
  49. sudo -u $cmdName touch $USER_AUTHORIZED_KEYS && chmod 600 $USER_AUTHORIZED_KEYS
  50. echo "mkdir $USER_AUTHORIZED_KEYS"
  51. fi
  52. # 生成ssh秘钥对
  53. if [ ! -f $USER_SSH_RSA_PRIVATE ]; then
  54. sudo -u $cmdName ssh-keygen -t rsa -f $USER_SSH_RSA_PRIVATE -P "" -q
  55. fi
  56. # 添加公钥
  57. if [ "$cmdAuthorizedKeys" ]; then
  58. echo "usshkey:" $cmdAuthorizedKeys
  59. echo $cmdAuthorizedKeys >> $USER_AUTHORIZED_KEYS
  60. fi
  61. }
  62. # 删除用户
  63. del_user() {
  64. grep "^$cmdName" $SYSPASSWD >& /dev/null
  65. if [ $? -ne 0 ]; then
  66. echo "${cmdName} is not exits"
  67. exit 1
  68. fi
  69. #
  70. unsudo_user
  71. userdel $cmdName && rm -r $USER_HOME_DIR
  72. if [ $? -eq 0 ];then
  73. echo "${cmdName} is delete successful"
  74. else
  75. echo "${cmdName} is delete failed"
  76. fi
  77. }
  78. # 添加sudo权限
  79. sudo_user() {
  80. grep "^$cmdName:" $SYSPASSWD >& /dev/null
  81. if [ $? -eq 0 ]; then
  82. echo "$cmdName ALL=(ALL) ALL" >> ${SUDOFILE} && echo "User $cmdName add sudo success!"
  83. else
  84. echo "User $cmdName not exists!"
  85. fi
  86. }
  87. # 取消sudo权限
  88. unsudo_user() {
  89. grep "${cmdName}" ${SUDOFILE} > /dev/null
  90. if [ $? -eq 0 ]; then
  91. sudo sed -i "/${cmdName}/d" ${SUDOFILE} && echo "User $1 on ${HOST} delete sudo success!"
  92. else
  93. echo "User $cmdName already delete sudo!"
  94. fi
  95. }
  96. check
  97. # 处理
  98. case $cmdOp in
  99. add)
  100. if [ -z $cmdName ] || [ -z $cmdPassword ]; then
  101. echo "add_user invalid params"
  102. exit 1
  103. fi
  104. add_user
  105. ;;
  106. del)
  107. if [ -z $cmdName ]; then
  108. echo "del_user invalid params"
  109. exit 1
  110. fi
  111. del_user
  112. ;;
  113. sudo)
  114. if [ -z $cmdName ]; then
  115. echo "sudo_user invalid params"
  116. exit 1
  117. fi
  118. sudo_user
  119. ;;
  120. unsudo)
  121. if [ -z $cmdName ]; then
  122. echo "unsudo_user invalid params"
  123. exit 1
  124. fi
  125. unsudo_user
  126. ;;
  127. *)
  128. exit 1
  129. ;;
  130. esac