zoukankan      html  css  js  c++  java
  • 添加用户

    #!/bin/sh
    #Author:woodman
    
    Files=userlist.txt
    Groups="operation"
    
    groupadd  -r $Groups &> /dev/null
    
    #user and password file
    userpass="`date +%Y%m%d`.$Groups"
    
    #function
    declare length=10
    declare uids=1000
    upass(){
    	for user in `cat $Files`
        do
           let uids=$uids+1
           Passwds="`< /dev/urandom tr -dc [:alnum:] | head -c $length `"
           ! id $user &>/dev/null && useradd $user -g $Groups -u $uids && echo $Passwds | passwd --stdin $Passwds &>/dev/null
           echo "$user:$Passwds" >> $userpass
    	done
    }
    mpass(){
        for user in `cat $Files`
        do
           let uids=$uids+1
           Passwds="`mkpasswd -s 0 -l $length`"
           ! id $user &>/dev/null && useradd $user -g $Groups -u $uids && echo $Passwds | passwd --stdin $Passwds &>/dev/null
           echo "$user:$Passwds" >> $userpass
    	done
    }
    
    #checking to see if the yanfa file exists.
      if [ ! -f $Files ];then
        echo "ERROR:$Files NOT exists!Please check it!"
        exit 1
      fi
    
    #Determine whether the command is installed
      mkpasswd -l 10  &>/dev/null
      Retval=$?
    
    #if Retval equal to 0 using mkpasswd to generate passwords
    #else using  urandom generation password
    
    case $Retval in 
    0) mpass;;
    *) upass;;
    esac
    
    
      echo "---------------Passwords Created Successfully!---------------"
      echo "The group $Groups added the following users:"
      cat $userpass
      echo "-------------------------THE END-----------------------------"
      exit 0
    

    用户文件列表userlist

    # cat userlist.txt 
    user01
    user02
    user03
    user04
    user05
    

    运行结果:

    ./addusers.sh
    ---------------Passwords Created Successfully!---------------
    The group operation added the following users:
    user01:XCVDysfNvq
    user02:RJHk1rHWCA
    user03:4QIa3DRXp4
    user04:b7UImz3ff5
    user05:NBhdfcAvXi
    -------------------------THE END-----------------------------

    用户uid和gid

    # id user03
    uid=1003(user03) gid=500(operation) groups=500(operation)
  • 相关阅读:
    [Java解惑]数值表达式
    Java使用LdAP获取AD域用户
    LDAP Error Codes
    Excel向上取整
    java中的三种取整函数
    Dwz手册的补充说明和常见问题
    【转】BSON数据格式
    go语言合并两个数组
    vscode远程修改文件('file': A system error occured )
    [转]Linux 桌面玩家指南:20. 把 Linux 系统装入 U 盘打包带走
  • 原文地址:https://www.cnblogs.com/woodman/p/6289285.html
Copyright © 2011-2022 走看看