Method for creating random numbers
1 ~~~~
/Dev/urandom
In Linux, a device/dev/urandom is used to generate a random number sequence. With this device, we can generate random strings as needed.
For example, if we want to generate a random password with 8-digit letters and numbers, we can:
[Linux @ test/tmp] $ cat/dev/urandom | head-1 | md5sum | head-c 8
6baf9282
2 ~~~~
In fact, linux already provides a system environment variable.
[chengmo@centos5 shell]$ echo $RANDOM
66918
[chengmo@centos5 shell]$ echo $RANDOM
10092
I may have doubts. How can I obtain a random number with more than five digits?
In the case of ten digits, use % to get the remainder of 10.
echo $((RANDOM%10))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
Vi passwd. sh
# Create a 10-digit random password...
#!/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
Run the script:
[Root @ 2 shell] # sh passwd. sh
BF8366c @ 13
Vi useradd. sh
# Create five accounts and reference random passwords.
#!/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
Run 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
# View results
[Root @ 2 shell] #Cat sumuserpasswd
Red1: $ Ca7 % 298d2
~~~~~~~~~~~~~
Red2: eEaBBB7Fb4
~~~~~~~~~~~~~
Red3: % 3e1_cece
~~~~~~~~~~~~~
Red4: 3 @ F % @ B0584
~~~~~~~~~~~~~
Red5: AdEe ^ 6BF $ F
# Test
[Root @ 2 shell] # su red1
[Red1 @ 2 shell] $ su red2
Password:
[Red2 @ 2 shell] $
You can also use html to display our results:
Html table code
<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>
You can direct your account and password to the webpage in html syntax.
Tempw.index.html
Echo "
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>
Echo "open index.html"