Linux 批量創(chuàng)建用戶 腳本

!/bin/bash

這支程式主要在幫您建立大量的帳號(hào)之用,更多的使用方法請(qǐng)參考:

http://linux.vbird.org/linux_basic/0410accountmanager.php#manual_amount

本程式為鳥哥自行開發(fā),在 CentOS 5.x 上使用沒有問題,

但不保證絕不會(huì)發(fā)生錯(cuò)誤!使用時(shí),請(qǐng)自行負(fù)擔(dān)風(fēng)險(xiǎn)~

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. 進(jìn)行帳號(hào)相關(guān)的輸入先!

echo ""
echo "例如我們崑山四技的學(xué)號(hào)為: 4960c001 到 4960c060 ,那麼:"
echo "帳號(hào)開頭代碼為 :4"
echo "帳號(hào)層級(jí)或年級(jí)為 :960c"
echo "號(hào)碼數(shù)字位數(shù)為(001~060):3"
echo "帳號(hào)開始號(hào)碼為 :1"
echo "帳號(hào)數(shù)量為 :60"
echo ""
read -p "帳號(hào)開頭代碼 ( Input title name, ex> std )======> " username_start
read -p "帳號(hào)層級(jí)或年級(jí) ( Input degree, ex> 1 or enter )=> " username_degree
read -p "號(hào)碼部分的數(shù)字位數(shù) ( Input # of digital )======> " nu_nu
read -p "起始號(hào)碼 ( Input start number, ex> 520 )========> " nu_start
read -p "帳號(hào)數(shù)量 ( Input amount of users, ex> 100 )=====> " nu_amount
read -p "密碼標(biāo)準(zhǔn) 1) 與帳號(hào)相同 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 "輸入的號(hào)碼不對(duì)啦!有非為數(shù)字的內(nèi)容!" ; exit 1
fi
if [ "$pwm" != "1" ]; then
pwm="2"
fi

2. 開始輸出帳號(hào)與密碼檔案!

[ -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è)定的位數(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. 開始建立帳號(hào)與密碼!

echo "帳密建置中,請(qǐng)稍待片刻!"
cat "$accountfile" | cut -d':' -f1 | xargs -n 1 useradd -m
chpasswd < "$accountfile"
pwconv
echo "OK!建立完成!"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 為何叫做 shell ? shell prompt(PS1) 與 Carriage Return(CR) 的關(guān)系?...
    Zero___閱讀 3,331評(píng)論 3 49
  • 找到了一個(gè)不錯(cuò)的學(xué)習(xí)Markdown語言的文檔,發(fā)在這里供自己隨時(shí)查閱,也希望能對(duì)大家有所幫助。想要速成的朋友可以...
    山公子閱讀 550評(píng)論 0 1
  • 前言 什么是CSS預(yù)處理器 定義:CSS預(yù)處理器定義了一種新的語言,其基本思想是,用一種專門的編程語言,為CSS增...
    SA_Arthur閱讀 3,210評(píng)論 0 18
  • Markdown 文件 NOTE: This is Traditional Chinese Edition Doc...
    onetao閱讀 367評(píng)論 0 0
  • 其實(shí)美國(guó)人民挺有意思的。 我第一次去學(xué)校的時(shí)候沒什么經(jīng)驗(yàn),穿著一條到膝蓋的小裙子就去上課。一開始動(dòng)筆就被太足的冷氣...
    疏安安安閱讀 378評(píng)論 0 0

友情鏈接更多精彩內(nèi)容