Shell中怎么創(chuàng)建用戶并生成隨機密碼,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)建隨機數(shù)的方法:
代碼如下:
1~~~~
/dev/urandom
在Linux中有一個設(shè)備/dev/urandom是用來產(chǎn)生隨機數(shù)序列的。利用該設(shè)備我們可以根據(jù)在需要生成隨機字符串。
比如我們要產(chǎn)生一個8位的字母和數(shù)字混合的隨機密碼,可以這樣:
代碼如下:
[linux@test /tmp]$ cat /dev/urandom | head -1 | md5sum | head -c 8
6baf9282
2~~~~
其實,linux已經(jīng)提供有個系統(tǒng)環(huán)境變量了。
代碼如下:
[chengmo@centos5 shell]$ echo $RANDOM
66918
[chengmo@centos5 shell]$ echo $RANDOM
10092
可能有疑問了,如果超過5位的隨機數(shù)怎么得到呢?
十位數(shù)的話,用%取10余數(shù)
代碼如下:
echo $((RANDOM%10))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vi passwd.sh
#創(chuàng)建一個 10 位的隨機的密碼。。。
#!/bin/bash
a=(a b c d e A B C D E F @ $ % ^ 0 1 2 3 4 5 6 7 8 9)
for ((i=0;i<10;i++));do
echo -n ${a[$RANDOM % ${#a[@]}]}
done
echo
執(zhí)行腳本:
代碼如下:
[root@2 shell]# sh passwd.sh
BF8366c@13
vi useradd.sh
#創(chuàng)建5個賬號,引用了隨機的密碼。
#!/bin/bash
i=1
while [ $i -le 5 ]
do
useradd red$i
a=`sh ./passwd.sh`
echo " red$i:$a " >> sumuserpasswd
echo "~~~~~~~~~~~" >> sumuserpasswd
echo red$i:$a|chpasswd
# echo $a | passwd –-stdin red"$i"
let i++
done
執(zhí)行:sh useradd.sh
代碼如下:
[root@2 shell]# cat /etc/passwd|grep red
red1:x:515:515::/home/red1:/bin/bash
red2:x:516:516::/home/red2:/bin/bash
red3:x:517:517::/home/red3:/bin/bash
red4:x:518:518::/home/red4:/bin/bash
red5:x:519:519::/home/red5:/bin/bash
#查看結(jié)果
[root@2 shell]# cat sumuserpasswd
red1:$Ca7%298d2
~~~~~~~~~~~~~
red2:eEaBBB7Fb4
~~~~~~~~~~~~~
red3:%3E385cecE
~~~~~~~~~~~~~
red4:3@F%@B0584
~~~~~~~~~~~~~
red5:AdEe^6BF$F
#測試一下
[root@2 shell]# su red1
[red1@2 shell]$ su red2
口令:
[red2@2 shell]$
也可以用html的方式來顯示我們的結(jié)果:
html表格代碼
代碼如下:
<body>
<tableborder='1'>
<tr>
<td>user</td>
<td>passwd</td>
</tr>
<tr>
<td>test1</td>
<td>123123</td>
</tr>
<tr>
<td>test2</td>
<td>aaabbb</td>
</tr>
</table>
</body>
可以把賬號和密碼以html語法的方式導(dǎo)向到網(wǎng)頁里面
代碼如下:
TEMP=index.html
echo "<html><body><h4>賬號和密碼</h4>" > $TEMP
echo "<tableborder=\"1\">" >> $TEMP
echo "<tr><td>username</td><td>password</td></tr>>> $TEMP
echo "<tr><td>$i</td><td>$a</td></tr>" >> $TEMP
echo "</table></body></html>" >>$TEMP
echo "open index.html"
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
網(wǎng)站標題:Shell中怎么創(chuàng)建用戶并生成隨機密碼-創(chuàng)新互聯(lián)
標題路徑:http://www.chinadenli.net/article8/dccoip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、云服務(wù)器、網(wǎng)站設(shè)計公司、Google、全網(wǎng)營銷推廣、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容