Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #!bin/bash
  2.  
  3. function check()
  4. {
  5.  
  6.  
  7. arg1=$1
  8. arg1="'"$arg1"'"
  9. arg2="/etc/$2"
  10. arg3="^$3"
  11. arg4=$4
  12.  
  13. commandLine="awk -F':' $arg1 $arg2 |grep $arg3 > $arg4"
  14.  
  15. eval $commandLine
  16.  
  17. return $?
  18. }
  19. function checkUserExists(){
  20. check 1 passwd $1 /dev/null
  21. return $?
  22. }
  23. function checkGroupExists(){
  24. check 1 group $1 /dev/null
  25. return $?
  26. [user1@localhost ~]$ vi check.sh
  27. [user1@localhost ~]$ more check.sh
  28. #!bin/bash
  29.  
  30. function check()
  31. {
  32.  
  33.  
  34. arg1=$1
  35. arg1="'"$arg1"'"
  36. arg2="/etc/$2"
  37. arg3="^$3"
  38. arg4=$4
  39.  
  40. commandLine="awk -F':' $arg1 $arg2 |grep $arg3 > $arg4"
  41.  
  42. eval $commandLine
  43.  
  44. return $?
  45. }
  46. function checkUserExists(){
  47. check 1 passwd $1 /dev/null
  48. return $?
  49. }
  50. function checkGroupExists(){
  51. check 1 group $1 /dev/null
  52. return $?
  53. }
  54. function checkUIDExists(){
  55. check 3 passwd $1 /dev/null
  56. return $?
  57. }
  58. function checkGroupUIDExists(){
  59. check 3 group $1 /dev/null
  60. return $?
  61. }
  62.  
  63. function usage(){
  64. echo "How to\n"
  65. echo "PARAMETERS : \n"
  66. echo "'-u' <user> : create user
  67. echo "'-ug' <user> <group> : create user and add it to the group"
  68. echo "Req : Launch the program as Administrator\n"
  69. }
  70.  
  71. function main(){
  72. if [[ $EUID -ne 0 ]] ; then
  73. echo "You must run this program as root !"
  74. exit 1
  75. else
  76. if [ $# -ne 3 -o $# -ne 2 ] ; then
  77. echo "Error in the parameters"
  78. exit 1
  79. else
  80. case "$1" in
  81. -u) if [ $# -e 2 ] ; then
  82. createUser $2
  83. else
  84. echo "Error incorrect number of param for '-u' , need a username"
  85. exit 3
  86. fi
  87. ;;
  88. -ug) echo "Not implemented yet"
  89. exit 4
  90. fi
  91. ;;
  92. *) echo "INVALID OPTION"
  93. exit 6
  94. ;;
  95. esac
  96. fi
  97. fi
  98. return 0
  99. }
  100.  
  101. function createUser(){
  102. checkUserExists $1
  103. if [ $? -e 0 ]; then
  104. uid=$(awk -F":" 'END { print $3 }' /etc/passwd)
  105. uid=$((uid+1))
  106.  
  107. if [ $uid -le 500 ]; then
  108. uid=501
  109. fi
  110. checkUIDExists() $uid
  111. if [ $? -ne 0 ]; then
  112. checkGroupUIDExists $uid
  113. fi
  114. while [ $? = 0 ]; do
  115. uid=$((uid+1))
  116. checkUIDExists $uid
  117. if [ $? -ne 0 ]; then
  118. checkGroupUIDExists $uid
  119. fi
  120. done
  121.  
  122. echo "$1:x:$uid:$uid:$1:/home/$1:/bin/bash" >> /etc/passwd
  123. echo "$1:x:$uid:" >> /etc/group
  124. mkdir /home/$1
  125. cp -vR /etc/skel/.bash* /home/$1
  126. chmod -vR 0755 /home/$1
  127. chown -vR $1:$1 /home/$1
  128. passwd $1
  129.  
  130. }
  131. main $1 $2 $3 $4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement