Set windows user python

how to create a new user in windows platform using python?

This is purposefully cryptic. You’ll need to study the Win32 package and ensure that you know what you are doing with Windows sys/admin.

The thing is: I don’t want to tell you something and have you break you machine.

This is purposefully cryptic. You’ll need to study the Win32 package and ensure that you know what you are doing with Windows sys/admin.

The thing is: I don’t want to tell you something and have you break you machine.

I can provide the missing information given a little background and more details of your environment. i.e. local machine vs domain, NT variant, etc.

I want to create user in win2k server, win 2k professional and win XP. if the user has already exists, I also need to be able to check and change its password.

now i want to change the IP related settings, could you please give me a function list or code sample?

I want to create user in win2k server, win 2k professional and win XP. if the user has already exists, I also need to be able to check and change its password.

now i want to change the IP related settings, could you please give me a function list or code sample?

  1. # BatchUserCreate.py
  2. #
  3. # A sample administrative script to perform a batch
  4. # creation of many users.
  5. # Input to this program is a text file with one user per
  6. # line. Each line contains the new username and the
  7. # user’s full name.
  8. # Creates the new user, and a new share on the server
  9. # for the user. The new share is secure, and can only
  10. # be accessed by the new user.
  11. import win32security, win32net, win32file, win32api
  12. import win32netcon, ntsecuritycon
  13. import os, sys, string
  14. # The name of the server to use to create the user.
  15. serverName = None
  16. # The logic for naming the home_drive assumes we have
  17. # a server name. If we dont, get the current machine name.
  18. if serverName is None:
  19. serverName = «\\\\» + win32api.GetComputerName()
  20. # The root of each users personal directory.
  21. # This is a local reference to the directory where each
  22. # personal directory is created.
  23. homeRoot = «C:\\Users»
  24. def CreateUserAndShare(userName, fullName):
  25. homeDir = «%s\\%s» % (serverName, userName)
  26. # Create user data in information level 3 (PyUSER_INFO_3) format.
  27. userData = <>
  28. userData[‘name’] = userName
  29. userData[‘full_name’] = fullName
  30. userData[‘password’] = userName
  31. userData[‘flags’] = win32netcon.UF_NORMAL_ACCOUNT | win32netcon.UF_SCRIPT
  32. userData[‘priv’] = win32netcon.USER_PRIV_USER
  33. userData[‘home_dir’] = homeDir
  34. userData[‘home_dir_drive’] = «P:»
  35. userData[‘primary_group_id’] = ntsecuritycon.DOMAIN_GROUP_RID_USERS
  36. userData[‘password_expired’] = 1 # User must change password next logon.
  37. # Create the user
  38. win32net.NetUserAdd(serverName, 3, userData)
  39. # Create the new directory, then the share
  40. dirName = os.path.join(homeRoot, userName)
  41. os.mkdir(dirName)
  42. shareData = <>
  43. shareData[‘netname’] = userName
  44. shareData[‘type’] = win32netcon.STYPE_DISKTREE
  45. shareData[‘path’] = dirName
  46. shareData[‘max_uses’] = -1
  47. # The security setting for the share.
  48. sd = CreateUserSecurityDescriptor(userName)
  49. shareData[‘security_descriptor’] = sd
  50. # And finally create it.
  51. win32net.NetShareAdd(serverName, 502, shareData)
  52. # A utility function that creates an NT security object for a user.
  53. def CreateUserSecurityDescriptor(userName):
  54. sidUser = win32security.LookupAccountName(serverName, userName)[0]
  55. sd = win32security.SECURITY_DESCRIPTOR()
  56. # Create the «well known» SID for the administrators group
  57. subAuths = ntsecuritycon.SECURITY_BUILTIN_DOMAIN_RID, \
  58. ntsecuritycon.DOMAIN_ALIAS_RID_ADMINS
  59. sidAdmins = win32security.SID(ntsecuritycon.SECURITY_NT_AUTHORITY, subAuths)
  60. # Now set the ACL, giving user and admin full access.
  61. acl = win32security.ACL(128)
  62. acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, sidUser)
  63. acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, sidAdmins)
  64. sd.SetSecurityDescriptorDacl(1, acl, 0)
  65. return sd
  66. # Debug helper to delete our test accounts and shares.
  67. def DeleteUser(name):
  68. try: win32net.NetUserDel(serverName, name)
  69. except win32net.error: pass
  70. try: win32net.NetShareDel(serverName, name)
  71. except win32net.error: pass
  72. try: os.rmdir(os.path.join(homeRoot, name))
  73. except os.error: pass
  74. if __name__==’__main__’:
  75. import fileinput # Helper for reading files line by line
  76. if len(sys.argv)
  77. print «You must specify an options file»
  78. sys.exit(1)
  79. if sys.argv[1]==»-delete»:
  80. for line in fileinput.input(sys.argv[2:]):
  81. DeleteUser(string.split(line,»,»)[0])
  82. else:
  83. for line in fileinput.input(sys.argv[1:]):
  84. userName, fullName = string.split(string.strip(line), «,»)
  85. CreateUserAndShare(userName, fullName)
  86. print «Created», userName

Источник

Читайте также:  Launch exe file java
Оцените статью