免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7907 | 回复: 6
打印 上一主题 下一主题

批量添加用户的脚本,大家提提意见 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-28 23:32 |只看该作者 |倒序浏览
好不容易放假了,重新写个批量添加用户的脚本。
思路有些混沌了,还望大家多多指教。

这个脚本还有很多地方需要改进,比如算法,比如数据的处理。
希望大家不吝提出修改意见。


  1. #!/bin/sh
  2. # Name: useraddmuti
  3. # Descripton: To add users to your system. Users can be list in a file.
  4. #             To exec this command your ID must be 0.
  5. # Author: PopZslam@Linux.net
  6. #-----------------------------------
  7. chkUID(){
  8.         getUID(){
  9.                 id|sed -e 's/(.*$//' -e 's/^uid=//'
  10.                 }

  11.         if [ "`getUID`" -ne 0 ]
  12.         then
  13.                 echo -e "\tYou are not root!"
  14.                 exit 0
  15.         fi
  16.         }
  17. chkUID
  18. usagePRT(){
  19.         echo ${USAGE:='USAGE:' `basename $0` '-f namelistfile'}
  20.         }
  21. chkFILE(){
  22.         if [ ! -z "`awk 'NF!=2{print NF;exit;}' $1`" ] && [ "`awk 'NF!=2{print NF;exit;}' $1`" -ne 2 ] ; then
  23.                 echo -e "The file's format is not right!"
  24.                 exit 0
  25.         fi
  26.         }

  27. userCHK(){
  28.         for USER in `awk '{print $1;}' $1`
  29.         do
  30.                 if grep -wq $USER /etc/passwd ; then
  31.                         echo -e "The user($USER) has been added!"
  32.                         exit 1
  33.                 fi
  34.                 if echo $USER|grep -wq "^[0-9].*" ; then
  35.                         echo -e "The user($USER)'s name is wrong format!"
  36.                         exit 1
  37.                 fi
  38.         done
  39.         }

  40. setOPT(){
  41.         echo -e "Now Let's set some options or you can use default settings."
  42.         setGRPNAME(){
  43.                 while :
  44.                 do
  45.                 echo -e "Would you like to add a new group to add these users to it?"
  46.                 echo -e "Enter YES to create a new group otherwise you must verify the group."
  47.                 printf "Your Answer: "
  48.                 read grpopt
  49.                 case $grpopt in
  50.                         yes)
  51.                         printf "Please enter the group's name: "
  52.                         read grpoptnew
  53.                         if cat /etc/group|sed 's/:.*//'|grep -wq $grpoptnew ; then
  54.                                 echo "The group's name($grpoptnew) exist."
  55.                                 exit
  56.                         else
  57.                                 grpname=$grpoptnew
  58.                                 echo -e "All these users will be added to group($grpname)..."
  59.                                 echo -e "Adding group ..."
  60.                                 if cp /etc/group /etc/group.$$ >; /dev/null 2>;&1 ; then
  61.                                         if groupadd $grpname ; then
  62.                                                 echo -e "The group($grpname) is added!"
  63.                                                 rm -f /etc/group.$$
  64.                                                 break 1
  65.                                         else
  66.                                                 echo -e "There's something wrong when adding the group($grpname)."
  67.                                                 echo -e " *** Please recovered the group file. *** "
  68.                                                 echo -e "You can cp /etc/group.$$ to /etc/group to recover."
  69.                                         fi
  70.                                 else
  71.                                         echo "Error! Please check the program or your disk space."
  72.                                         exit 0
  73.                                 fi
  74.                         fi
  75.                         ;;
  76.                         *) : ;;
  77.                 esac
  78.                 done
  79.                 }
  80.         setGRPNAME
  81.         }

  82. addUSER(){
  83.         if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then

  84.         for user in `sed 's/ .*//' $1`
  85.         do
  86.         pass=`awk '{
  87.          $1~/$name/
  88.          {print $2;exit}
  89.          } name=$user' $1`
  90.         if [ -z "$pass" ] ; then
  91.                 echo -e "The passwd is used by default sun123."
  92.                 pass=sun123
  93.         fi
  94.         if [ ${#pass} -lt 6 ] ; then
  95.                 echo -e "The user($user)'s password is too short!"
  96.                 echo -e "Use default password: sun123."
  97.                 pass=sun123
  98.         fi
  99.         if useradd $user ; then
  100.                 echo -e "The user($user) is added."
  101.                         if echo $pass|passwd $user --stdin >; /dev/null 2>;&1 ; then
  102.                                 echo -e "The user($user)'s password is setted!"
  103.                         else
  104.                                 echo -e "The user($user)'s password is NOT set!"
  105.                         fi
  106.         else
  107.                 echo -e "The user($user) is NOT add."
  108.         fi
  109.         done
  110.         rm -f /etc/passwd.$$ /etc/shadow.$$
  111.         else
  112.                 echo -e "There something wrong when backup the passwd and shadow file."
  113.         fi
  114.         }

  115. if [ $# -ne 2 ] ;  then
  116.         usagePRT
  117.         exit 0
  118. fi

  119. case "$1" in
  120.         -f)
  121.         if [ -f "$2" ] ; then
  122.                 echo -e "Reading usernamelist file""("$2")" "..."
  123.                 chkFILE $2
  124.                 userCHK $2
  125.                 setOPT
  126.                 addUSER $2
  127.         else
  128.                 echo -e "There's no usernamelist file!"
  129.         fi
  130.         ;;
  131.         *) usagePRT
  132.         exit 0
  133.         ;;
  134. esac
复制代码

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-01-29 00:13 |只看该作者

批量添加用户的脚本,大家提提意见

走召弓虽!!!

论坛徽章:
0
3 [报告]
发表于 2005-01-29 00:35 |只看该作者

批量添加用户的脚本,大家提提意见

[quote]原帖由 "寂寞烈火"]走召弓虽!!![/quote 发表:



忘记把这些用户添加到同一个组了。


  1. if useradd $user ; then
复制代码

应该替换为

  1. if useradd $user -g $grpname ; then
复制代码

论坛徽章:
0
4 [报告]
发表于 2005-01-29 21:56 |只看该作者

批量添加用户的脚本,大家提提意见

newusers

论坛徽章:
0
5 [报告]
发表于 2005-06-03 17:40 |只看该作者

批量添加用户的脚本,大家提提意见

高手谢谢了

论坛徽章:
0
6 [报告]
发表于 2005-06-03 17:46 |只看该作者

批量添加用户的脚本,大家提提意见

[quote]原帖由 "寂寞烈火"]走召弓虽!!![/quote 发表:


??没懂

论坛徽章:
0
7 [报告]
发表于 2005-06-08 22:29 |只看该作者

批量添加用户的脚本,大家提提意见

发现这个老贴居然成了精华,呵呵。

高兴的顶一把……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP