xiongyongli

批量新建账号模板---(适用与passwd --stdin参数)

vi account1.sh
 #!/bin/bash
 #这个程序用来新建账号,功能有:
 #1.检查account1.txt是否存在,并将该文件内的账号取出;
 #2.新建上述文件的账号
 #3.将上述账号的密码修改成为强制第一次进入需要修改密码的格式。
 #2017/11/1 xyl
 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
 #检查account1.txt是否存在
 if [ ! -f account1.txt ]; then
      echo "所需要的账号文件不存在,请新建account1.txt,每一行一个账号名称“
      exit 1
fi 

usernames=$ (cat account1.txt)
for username in $usernames
do 
        useradd $username
        echo $username | passwd --stdin $username
        chage -d 0 $username
done

  接下来只要新建account1.txt文件即可。每一行一个账号。

 

 

批量创建新账号的范例(适用于连续数字,如学号)

本实例的特点:

 1.默认不允许使用纯数字方式

 2.可加入年级来区分账号

3.可设置账号的其实号码与账号数量

4.有两种密码的新建方式,可以与账号相同或程序自行以随机数新建密码文件

#!/bin/bash
#新建大量账号
#History :
#2017/11/1 xyl
export LANG_zh_TW.big5
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
accountfile="user.passwd"

#1.进行账号相关的输入
echo ""
echo "例如我们学校学生学号为4960c001到4960c060,那么:”
echo "账号开头代码为 :4"
echo "账号层级或年级为 :960c"
echo "号码数字位数为(001~060) : 3"
echo "账号开始号码为 :1"
echo "账号数量为 : 60"
echo ""
read -p "账号开头代码(Input title name,ex>std)======>" username_start
read -p "账号层级或年级(Input degree,ex>1 or enter)==>" username_degree
read -p "号码部分的数字位数(Input \# of digital)======>" nu_nu
read -p "起始号码(Input start number,ex>520)========>" nu_start
read -p "账号数量(Input amout of users,ex>100)======>" nu_amount
read -p "密码标准1)与账号相同2)随机数自定义===========>" pwm
if [ "$username_start" == "" ];then
  echo "没有输入开头的代码,不执行!";exit 1
fi
#判断数字系统
testing0=$(echo $nu_nu | grep \'[^0-9]\')
testing1=$(echo $nu_amount | grep \'[^0-9]\')
testing2=$(echo $nu_start | grep \'[^0-9]\')
if ["$testing0"!="" -o "$testing1"!="" -o "$testing2"!=""];then
  echo "输入的号码不对,有非数字的内容!";exit 1
fi
if ["$pwm"!="1"];then
  pwm="2"
fi
#2.开始输出账号与密码文件
[ -f "$accountfile"]&&mv $accountfile "$accountfile"$(date +%Y%m%d)
nu_end=$(($nu_start+$nu_amount-1))
for((i=$nu_start;i<$nu_end;i++))
do
  nu_len=${#1}
  if [$nu_nu -lt $nu_len];then
    echo "数值的位数($i->$nu_len)已经比你设置的位数大"
    echo "程序无法继续"
    exit 1
  fi
  nu_diff=$(($nu_nu - $nu_len))
  if ["$nu_diff"!="0"];then
    nu_nn=0000000000
    nu_nn=${nu_nn:1:$nu_diff}
  fi
  account=${username_start}${username_degree}${nu_nn}${i}
  if ["$pwm"=="1"];then
    password="$account"
  else
    password=$(openssl rand -base64 6)
  fi
  echo "$account":"$password" | tee -a "$accountfile"
done

#3.开始新建账号与密码
cat "$accountfile" | cut -d\':\' -f1 |xargs -n 1 useradd -m
chpasswd<"$accountfile"
pwconv
echo "OK!新建完成!"

#如果要新建同一班级具有同一组可以新用groupadd建立组,再
cat "$accountfile" | cut -d\':\' -f1 |xargs -n 1 useradd -m -g groupname


如果想将新建的用户整个删除,可以用下列的脚本
vi delaccount2.sh
#!/bin/bash
username=$(cat user.passwd |cut -d \':\' -f 1)
for username in $usernames
do
  echo "userdel -r $username"
  userdel -r $username
done

  

分类:

技术点:

相关文章: