pwgen和mkpasswd都有自己动生成密码文件的功能。而且mkpasswd可以自动更改密码到指定用户。
<strong>两者的区别如下:</strong>
1、pwgen的用法
<br />
Usage: pwgen [ OPTIONS ] [ pw_length ] [ num_pw ] Options supported by pwgen: -c or --capitalize Include at least one capital letter in the password -A or --no-capitalize Don't include capital letters in the password -n or --numerals Include at least one number in the password -0 or --no-numerals Don't include numbers in the password -y or --symbols Include at least one special symbol in the password -s or --secure Generate completely random passwords -B or --ambiguous Don't include ambiguous characters in the password -h or --help Print a help message -H or --sha1=path/to/file[#seed] Use sha1 hash of given file as a (not so) random generator -C Print the generated passwords in columns -1 Don't print the generated passwords in columns -v or --no-vowels Do not use any vowels so as to avoid accidental nasty words
2、mkpasswd的用法
<br />
mkpasswd的用法可以使用man mkpasswd查看,这里就不列了,用法大致如下:
<br />
#长度为20的密码 mkpasswd -l 20 #长度为15的密码,并且含有5个数字 mkpasswd -l 15 -d 5 #长度为15的密码,并且含有5个数字,并指定更改的用户为test mkpasswd -l 15 -d 5 test #最后搞个暴力的,定义长度10,含有5个数字和5个特殊字符。 mkpasswd -l 10 -d 5 -s 5
<br />
<br />
<strong>1、</strong><strong>利用pwgen自动更改密码,并发送到相关邮件中。</strong>
<br />
#!/bin/bash cd /root/test if [ -f "passfile" ];then mv passfile passfile.$(date +%Y%m%d) fi passfile=/root/test/passfile >$passfile #如果不存在新建该文件 list="abc test vie" for user in $list do if ! grep -w ^$user /etc/passwd > /dev/null then echo "user NOT present: $user" else echo "user present: $user" pass=$(/usr/local/bin/pwgen -1 -sy 16) #随机生成一个密码文件,且至少包含一个特别字符 echo "$user:$pass">>$passfile fi done cat $passfile | /usr/sbin/chpasswd echo "jiqiming or IP address " >> $passfile mail -s mima xxxxxxxxxxx@139.com < /root/test/passwd
<br />
<span style="color:#e53333;">若使用mkpasswd,则将上面的脚本中的pwgen换成mkpasswd,后面的参数也相应的改下。</span>脚本很简单,不多介绍。
一般mkpasswd是系统默认安装的,pwgen需要另外安装,地址如下:
<a href="http://ncu.dl.sourceforge.net/project/pwgen/pwgen/2.06/pwgen-2.06.tar.gz">http://ncu.dl.sourceforge.net/project/pwgen/pwgen/2.06/pwgen-2.06.tar.gz</a>
<br />
<strong>2、</strong><strong>另外不利用上面两个工具,我们也可以直接通过脚本实现,如</strong><strong>下:</strong>
定期自动更改密码并发送邮件的小脚本
<br />
#!/bin/bash tr -dc _A-Z-a-z#$%^*-0-9 /home/1.txt cat /home/1.txt |passwd root --stdin SendStatus=`mail -v -s "test mail" xxxxx@139.com < /home/1.txt | grep -c "Sender ok" ` rm -rf /home/1.txt if [ "$SendStatus" == "1" ] ; then echo "Sender mail ok" else echo "Sender mail fail!" sleep 50 sh /home/ceshi.sh fi
<br />
上面无非是提供了三种工具实现密码文件的生成,并利用其来更改密码。具体修改的思路是大同小异的——都是利用shell脚本来批量完成。