The following code is tested under Python 2.6.6
Add an account and change your password:
(Note: Linux expect command can also be completed, interactive auto-input function)
#!/usr/bin/env pythonaccount = ' sl_t1 ' passwd= ' a1p2p3l4e5 ' cmd_useradd = ' useradd %s ' % (account) #添加用户命令, modify Import subprocess as as needed spdef useradd ():cmd = ' export lc_all=en_us && %s ' % (cmd _useradd) F=open ('/dev/null ', ' W ') ret = sp.call (cmd, shell=true, stdout=f,  STDERR=F) F.close () return retdef passwd ():cmd = ' export lc_all=en_us & & passwd %s ' % (account) F=open ('/dev/null ', ' W ') p = sp. Popen (CMD, SHELL=TRUE, STDIN=SP. PIPE, STDOUT=F, STDERR=F) p.stdin.write ("%s\n" % (passwd)) p.stdin.write ("%s\n" % (passwd)) F.close () return p.wait () Def pymain (): #print ' useradd: ', useradd () #print ' passwd : ', passwd () ret = useradd () if not 0==ret:print ' UserAdd ', retreturnret = passwd () if not 0==ret:print ' passwd ',retreturn print ' Done ' #如果工作正常返回done, otherwise failed to return the steps and error code if __name__== ' __main__ ':p ymain ()
[Python] Operations auxiliary scripts