Linux批量添加刪除用戶

添加用戶

. /etc/init.d/functions #調用系統庫
> /root/shell/user.txt #清空文件
>/root/shell/false_user.txt
for n in $(seq -w 10) #10個用戶名產生
do 
    passwd=`echo $(date +%t%N) $RANDOM |md5sum|cut -c 3-10` #隨機產生密碼並截取8個
        useradd zxc-$n >&/dev/null && user_status=$?
    #添加用戶
    echo "$passwd"|passwd --stdin zxc-$n >&/dev/null && pass_status=$?
    #判斷是否執行成功,分別寫入TXT文件中
    if [ $user_status -eq 0 -a $pass_status -eq 0 ]
    then 
        action "useradd zxc-$n" /bin/true
        #調用系統庫的方法
        echo -e "user:\t zxc-$n passwd: \t $passwd ">>/root/shell/user.txt
    else 
        action "useradd zxc-$n" /bin/false
            echo -e "user:\t zxc-$n passwd: \t $passwd ">>/root/shell/false_user.txt
    fi   

done 

刪除用戶

#!/bin/bash
 . /etc/init.d/functions
for n in $(seq -w 10)
do
    #echo zxc-$n
    userdel  zxc-$n && del_stastus=$?
    if [ $del_stastus -eq 0 ]
    then
          action "userdel zxc-$n" /bin/true
    else 
          action "userdel zxc-$n" /bin/false
    fi 
done 

這兩個程序大同小異,其實可以寫在一個文件中,用case判斷執行的是添加還是刪除

#!/bin/bash 
. /etc/init.d/functions
> /root/shell/user.txt
>/root/shell/false_user.txt
RE=0
case "$1" in 
add)
    for n in $(seq -w 10)
    do 
        passwd=`echo $(date +%t%N) $RANDOM |md5sum|cut -c 3-10`
            useradd zxc-$n >&/dev/null && user_status=$?

        echo "$passwd"|passwd --stdin zxc-$n >&/dev/null && pass_status=$?

        if [ $user_status -eq 0 -a $pass_status -eq 0 ]
        then 
            action "useradd zxc-$n" /bin/true
            echo -e "user:\t zxc-$n passwd: \t $passwd ">>/root/shell/user.txt
        else 
            action "useradd zxc-$n" /bin/false
            echo -e "user:\t zxc-$n passwd: \t $passwd ">>/root/shell/false_user.txt
        fi   

    done 
    ;;
del) 
    for n in $(seq -w 10)
    do
        #echo zxc-$n
        userdel  zxc-$n && del_stastus=$?
        if [ $del_stastus -eq 0 ]
        then
              action "userdel zxc-$n" /bin/true
        else 
              action "userdel zxc-$n" /bin/false
        fi 
    done 
    ;;
*)      
    echo "Usage:$0 {add|del}"
    exit 
    ;;
esac 
exit $RE

( 寫於2016年5月15日,http://blog.csdn.net/bzd_111

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章