一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python控制windows加域的示例

时间:2014-03-13 编辑:简简单单 来源:一聚教程网

刚写完,还没来得及测试,依赖的是netdom,如需要软件包,可留言

 代码如下 复制代码

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%system32en-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)

热门栏目