Import pythoncom Import WMI Import OS Import Platform def joindomain (Domain,username,password,dns): If not domain or not username or not password or not DNS: Return False # Change the work directory Os.chdir (Os.path.dirname (__file__)) Currpath = Os.path.dirname (Os.path.abspath (__file__)) # Get OS Details OsInfo = Platform.uname () If not osinfo[0] = = ' Windows ': Print "This feature only supports windows!" Return False CmdLine = None If osinfo[2].lower () = = ' XP ': # Windows XP Netdomexe = Os.path.join (Currpath, "netdom", "XP", "Netdom.exe") If not os.path.exists (Netdomexe): Print "Netdom.exe not found!" Return False CmdLine = "./netdom/xp/netdom Join%s/domain:%s/userd:%s@%s/passwordd:%s"% (Osinfo[1],domain,username,domain, Password Elif osinfo[2].lower () = = "7" and osinfo[4] = = ' x86 ': # windows 7 32bit Netdomexe = Os.path.join (Currpath, "netdom", "x86", "Netdom.exe") Netdommui = Os.path.join (Currpath, "netdom", "x86", "Netdom.exe.mui") If not os.path.exists (Netdomexe) or not os.path.exists (Netdommui): Print "Netdom.exe or Netdom.exe.mui not found!" Return False #copy the files CmdLine = ' Copy '%s ' '%windir%system32 '/y '% netdomexe Os.system (CmdLine) CmdLine = ' Copy '%s ' '%windir%system32zh-cn '/y '% netdommui Os.system (CmdLine) CmdLine = ' Copy '%s ' '%windir%system32en-us '/y '% netdommui Os.system (CmdLine) CmdLine = "netdom join%s/domain:%s/userd:%s@%s/passwordd:%s"% (Osinfo[1],domain,username,domain,password) Elif osinfo[2].lower () = "7" and osinfo[4] = = ' AMD64 ': # windows 7 64bit Netdomexe = Os.path.join (Currpath, "netdom "," x64 "," Netdom.exe ") Netdommui = Os.path.join (Currpath," netdom "," x64 "," Netdom.exe.mui ") If not Os.path.exists (Netdomexe) or not os.path.exists (netdommui): print ' Netdom.exe or Netdom.exe.mui not found! ' Return False #copy the files cmdline = ' copy '%s ' '%windir%system32 '/y '% netdomexe Os.system (cmdline) cmdline = ' copy '%s '%windir%system32zh-cn '/y '% netdommui Os.system (cmdline) CmdLine = ' copy '%s '%windir%s ystem32en-us "/y '% netdommui Os.system (cmdline) CmdLine =" netdom join%s/domain:%s/userd:%s@%s/passwordd:%s " % (Osinfo[1],domain,username,domain,password) # Change the DNS server Wmiservice = WMI. WMI () Colnicconfigs = wmiservice.win32_networkadapterconfiguration (ipenabled = True) Objnicconfig = Colnicconfigs[0] returnvalue = Objnicconfig.setdnsserversearchorder (DNSServerSearchOrder = [DNS]) If not returnvalue[0] = = 0 or not returnvalue[1] = = 1: Print "Set DNS server Failed" Return False # Flush DNS Os.system ("Ipconfig/flushdns") # Execute the cmdline of join domain If CmdLine = None: Os.system (CmdLine) |