老用管道去改密码怕history记录下来,偶尔看见usermod实际是有-p参数指定密码的,问了度娘,终于知道怎么用的了
用python得到加密后字符串
# python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import crypt>>> print crypt.crypt('password','12')12CsGd8FRcMSM
添加用户时指定密码
# useradd testuser -p 12CsGd8FRcMSM
测试
[root@ops1 ~]# su testuser[testuser@ops1 root]$ su testuserPassword: #输入password[testuser@ops1 root]$
####python脚本
# cat getcryptpwd.py
#!/usr/bin/env pythonimport cryptimport randomchars='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'str=''str += chars[random.randint(0,len(chars))]str += chars[random.randint(0,len(chars))]pw=raw_input("Please Enter Password:")cryptpw=crypt.crypt(pw,str)print "Cryptd Password is : " + cryptpw