Linux批量新增刪除使用者
阿新 • • 發佈:2019-02-02
新增使用者
. /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