+ -
当前位置:首页 → 问答吧 → 如何利用python在linux上添加用户,并修改密码~谢谢

如何利用python在linux上添加用户,并修改密码~谢谢

时间:2010-11-07

来源:互联网

比如添加用户为 test, 设置密码为 123456
  1. import os
  2. os.system('useradd -m test')
  3. os.system('passwd test')
复制代码
然后怎么和系统交互进行密码设定呢~

作者: 我爱吃泥巴   发布时间: 2010-11-07

用VTE可心不?
  1. #!/usr/bin/env python

  2. import os
  3. import gtk
  4. import vte
  5. import time
  6. from subprocess import Popen, PIPE

  7. def show_callback(terminal):
  8.     terminal.feed_child('cd /\n')
  9.     terminal.feed_child('whoami\n')
  10.     terminal.feed_child('echo test\n')
  11.     for ii in range (3):
  12.         terminal.feed_child('echo ' + str(ii + 1)  + '\n')
  13.     terminal.feed_child('vi http.py\n')

  14. def write(terminal, text):
  15.     x, y = terminal.get_cursor_position()
  16.     terminal.feed(text + '\n', len(text) + 1)

  17. window = gtk.Window()
  18. window.connect('destroy', lambda w: gtk.main_quit())

  19. terminal = vte.Terminal()
  20. terminal.connect("show", show_callback)

  21. child_pid = terminal.fork_command()

  22. #write(terminal, '1234567890')

  23. window.add(terminal)
  24. window.show_all()
  25. gtk.main()
复制代码

作者: icyomik   发布时间: 2010-11-08