Python控制windows加域 | 马犇-技术博客

Python控制windows加域

来源:本站原创 Python, 原创 超过7,594 views围观 2条评论

测试OK,依赖的是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
    currPath = os.path.dirname(os.path.abspath(__file__))
    os.chdir(currPath)
    # 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%%\system32\zh-CN" /y' % netdomMui
        os.system(cmdline)
        cmdline = 'copy "%s" "%%windir%%\system32\en-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%%\system32\zh-CN" /y' % netdomMui
        os.system(cmdline)
        cmdline = 'copy "%s" "%%windir%%\system32\en-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,"114.114.114.114"])
    print returnValue
    if not returnValue[0] == 0 and not returnValue[0] == 1:
        print "Set DNS server failed"
        return False
    # flush dns
    os.system("ipconfig /flushdns")

    # execute the cmdline of join domain
    if not cmdline == None:
        os.system(cmdline)
    return True

if __name__ == '__main__':
    joinDomain("maben.com","administrator","1234abc","10.3.3.163")
本文链接:http://www.maben.com.cn/archives/645.html转载请注明出处