linux中怎样建立批量用户,Linux 大批量建立用户
是因為我是直接以 useradd 來新增的, 所以,即使不了解 UID ,也是可以適用的啦~整支程序的特色是:
? 默認不允許純數(shù)字方式建立賬號;
? 可加入年級來區(qū)分賬號;
? 可設定賬號的起始號碼與賬號數(shù)量;
? 有兩種密碼建立方式,可以與賬號相同或程序自行以隨機數(shù)建立密碼文件。
執(zhí)行方法也簡單的要命~請自行參考的啦!不再多說~使用時請注意,不要在公家使用的主機上面進行測試,因為..... 這支
程序會大量建立賬號嘛!^_^
#!/bin/bash
#
# 這支程序主要在幫您建立大量的賬號之用,更多的使用方法請參考:
#
#
# 本程序為鳥哥自行開發(fā),在 CentOS 5.x 上使用沒有問題,
# 但不保證絕不會發(fā)生錯誤!使用時,請自行負擔風險~
#
# History:
# 2005/09/05 VBird 剛剛才寫完,使用看看先~
# 2009/03/04 VBird 加入一些語系的修改與說明,修改密碼產(chǎn)生方式 (用
openssl)
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 "號碼數(shù)字位數(shù)為(001~060):3"
echo "賬號開始號碼為 :1"
echo "賬號數(shù)量為 :60"
echo ""
read -p "賬號開頭代碼 ( Input title name, ex> std )======> " username_start
read -p "賬號層級或年級 ( Input degree, ex> 1 or enter )=> " username_degree
read -p "號碼部分的數(shù)字位數(shù) ( Input \# of digital )======> " nu_nu
read -p "起始號碼 ( Input start number, ex> 520 )========> " nu_start
read -p "賬號數(shù)量 ( Input amount of users, ex> 100 )=====> " nu_amount
read -p "密碼標準 1) 與賬號相同 2)隨機數(shù)自定義 ==============> " pwm
if [ "$username_start" == "" ]; then
echo "沒有輸入開頭的代碼,不給你執(zhí)行哩!" ; exit 1
fi
# 判斷數(shù)字系統(tǒng)
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 "輸入的號碼不對啦!有非為數(shù)字的內(nèi)容!" ; 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=${#i}
if [ $nu_nu -lt $nu_len ]; then
echo "數(shù)值的位數(shù)($i->$nu_len)已經(jīng)比你設定的位數(shù)($nu_nu)
還大!"
echo "程序無法繼續(xù)"
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 』那行!這支腳本可以在底下連結下載:
?
如果僅是測試而已,想要將剛剛建立的使用者整個刪除,則可以使用如下的腳本來進行刪除!
[root@www ~]# vi delaccount2.sh
#!/bin/bash
usernames=$(cat user.passwd | cut -d ':' -f 1)
for username in $usernames
do
echo "userdel -r $username"
userdel -r $username
done
[root@www ~]# sh delaccount2.sh
總之,賬號管理是很重要的!希望上面的說明能夠對大家有點幫助啦!
閱讀(357) | 評論(0) | 轉發(fā)(0) |
總結
以上是生活随笔為你收集整理的linux中怎样建立批量用户,Linux 大批量建立用户的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是外盘和内盘 卖方价格成交的纳入“外
- 下一篇: 手机怎么转账给别的卡