from pprint import pprint import winreg import ctypes import win32com.client import platform wbemFlagReturnImmediately = 0x10 wbemFlagForwardOnly = 0x20 Name_PC =platform.node() # #FUNCTION# ==================================================================================================================== # Name...........: _DisableNetAdapter # Description....: Disables the specified network adapter. # Syntax.........: _DisableNetAdapter($sNetAdapter) # Parameters.....: $sNetAdapter - Name of the network adapter. # The Windows network connection name can be used instead of network adapter. # Return values..: Success - 1 # Failure - 0 # =============================================================================================================================== def DisableNetAdapter(NetAdapter): wmi_instance = win32com.client.GetObject(r"winmgmts:\\"+ Name_PC + r"\root\cimv2") if wmi_instance == 0: return 0 adapterName = GetNetworkAdapterFromID(NetAdapter) QueryNetAdapterConfig = str("select * from Win32_NetworkAdapter Where Name = '" + adapterName + "'") colNetAdapterConfig = wmi_instance.ExecQuery(QueryNetAdapterConfig,"WQL",(wbemFlagReturnImmediately+wbemFlagForwardOnly)) if not type(colNetAdapterConfig) is win32com.client.CDispatch: return 0 for objNetAdapter in colNetAdapterConfig: #try: return_obj = ctypes.c_uint32(objNetAdapter.Disable()).value if return_obj == 0: return 1 #except: # pass return 0 # #FUNCTION# ==================================================================================================================== # Name...........: _EnableNetAdapter # Description....: Enables the specified network adapter. # Syntax.........: _EnableNetAdapter($sNetAdapter) # Parameters.....: $sNetAdapter - Name of the network adapter. # The Windows network connection name can be used instead of network adapter. # Return values..: Success - 1 # Failure - 0 # =============================================================================================================================== def EnableNetAdapter(NetAdapter): wmi_instance = win32com.client.GetObject(r"winmgmts:\\"+ Name_PC + r"\root\cimv2") if wmi_instance == 0: return 0 adapterName = GetNetworkAdapterFromID(NetAdapter) if adapterName: NetAdapter = adapterName QueryNetAdapterConfig = str("select * from Win32_NetworkAdapter Where Name = '" + NetAdapter + "'") colNetAdapterConfig = wmi_instance.ExecQuery(QueryNetAdapterConfig,"WQL",(wbemFlagReturnImmediately+wbemFlagForwardOnly)) if not type(colNetAdapterConfig) is win32com.client.CDispatch: return 0 for objNetAdapter in colNetAdapterConfig: try: return_obj = objNetAdapter.Enable() if return_obj == 0: return 1 except: pass return 0 # #FUNCTION# ==================================================================================================================== # Name...........: _GetNetworkAdapterList # Description....: Get a list of Ethernet network adapter installed on the system # Syntax.........: _GetNetworkAdapterList() # Return values..: Success - Returns a 2 dimensional array. # element[n][0] - Name of the network adapter # element[n][1] - Name of the network ID # Failure - 0 # =============================================================================================================================== def GetNetworkAdapterList(): Reg = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE) Key = winreg.OpenKey(Reg,r"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces") Guid = [];index = [];list1 = [];list2 = [];i = 0 while 1: try: Guid.append(winreg.EnumKey(Key,i).upper()) i += 1 except: break winreg.CloseKey(Key) i = 0 while 1: index.append(winreg.EnumKey((winreg.OpenKey(Reg,r"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\")),i)) try: for j in range(len(Guid)): if (winreg.QueryValueEx(winreg.OpenKey(Reg,(r"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + str(index[i]))),"NetCfgInstanceId")[0]== Guid[j]): list1.append(winreg.QueryValueEx(winreg.OpenKey(Reg,(r"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"+ str(index[i])) ,1),"DriverDesc")[0]) list2.append(winreg.QueryValueEx(winreg.OpenKey(Reg,(r"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"+ str(Guid[j]) + "\\Connection") ,1),"Name")[0]) except: break i += 1 return tuple(zip(list1,list2)) # #FUNCTION# ==================================================================================================================== # Name...........: _FlushDNS # Description....: Reset the client resolver cache. # Syntax.........: _FlushDNS # Return values..: Success - 1 # Failure - 0 # =============================================================================================================================== def FlushDNS(): FlushDNS = ctypes.cdll.LoadLibrary("dnsapi.dll") try: Retour_Flush = FlushDNS.DnsFlushResolverCache return(bool(Retour_Flush)) except: return 0 # #FUNCTION# ==================================================================================================================== # Name...........: _FlushDNSEntry # Description....: Remove the specified entry from the client resolver cache. # Syntax.........: _FlushDNS # Return values..: Success - 1 # Failure - 0 # =============================================================================================================================== def FlushDNSEntry(sHost): FlushDNSEntry = ctypes.cdll.LoadLibrary("dnsapi.dll") try: FlushDNSEntry = FlushDNSEntry.DnsFlushResolverCacheEntry_W((sHost.c_wchar)) return(int(FlushDNSEntry)) except: return 0 # #FUNCTION# ==================================================================================================================== # Name...........: _GetNetworkAdapterFromID # Description....: Get the network card name from its Windows network connection name # Syntax.........: _GetNetworkAdapterFromID($sNetworkID) # Parameters.....: $sNetworkID - Name of the network connection (ex: Local Area Network). # Return values..: Success - Returns the network adapter name # Failure - 0 # =============================================================================================================================== def GetNetworkAdapterFromID(NetworkID): NetworkList = GetNetworkAdapterList() if NetworkList == 0: return 0 for i in range(len(NetworkList)): if NetworkList[i][1] == NetworkID: return NetworkList[i][0] return 0 # #FUNCTION# ==================================================================================================================== # Name...........: _GetNetworkGUI # Description....: Get the network ID stored in the system registry for the specified network adapter (GUID) # Syntax.........: _GetNetworkGUI($sNetAdapter) # Parameters.....: $sNetAdapter - Name of the network adapter # Return values..: Success - Returns the network GUID # Failure - 0 # =============================================================================================================================== def GetNetworkGUI(NetAdapter): Reg = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE) index = [] i = 0 while 1: index.append(winreg.EnumKey((winreg.OpenKey(Reg,r"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\")),i)) try: if (winreg.QueryValueEx(winreg.OpenKey(Reg,(r"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + str(index[i]))),"DriverDesc")[0]== NetAdapter): return winreg.QueryValueEx(winreg.OpenKey(Reg,(r"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + str(index[i]))),"NetCfgInstanceId")[0] except: break i += 1 return 0 # #FUNCTION# ==================================================================================================================== # Name...........: _EnableDHCP # Description....: Enables the Dynamic Host Configuration Protocol (DHCP) for IP configuration. # Syntax.........: _EnableDHCP($sNetAdapter) # Parameters.....: $sNetAdapter - Name of the network adapter. # The Windows network connection name can be used instead of network adapter. # Return values..: Success - 1 or 2 (reboot required) # Failure - 0 and sets @error (see remarks) # # Remarks........: When the function fails (returns 0) @error contains extended information (see global remarks). # =============================================================================================================================== def EnableDHCP(NetAdapter): wmi_instance = win32com.client.GetObject(r"winmgmts:\\"+ Name_PC + r"\root\cimv2") if wmi_instance == 0: return 0 adapterName = GetNetworkAdapterFromID(NetAdapter) if adapterName: NetAdapter = adapterName QueryNetAdapterConfig = str("select * from Win32_NetworkAdapterConfiguration Where Caption like '%" + NetAdapter + "'") colNetAdapterConfig = wmi_instance.ExecQuery(QueryNetAdapterConfig,"WQL",(wbemFlagReturnImmediately+wbemFlagForwardOnly)) if not type(colNetAdapterConfig) is win32com.client.CDispatch: return 0 for objNetAdapter in colNetAdapterConfig: try: return_obj = objNetAdapter.EnableDHCP() if EnableDHCP_DNS(NetAdapter): if return_obj == 0: return 1 if return_obj == 1: return 2 except: pass # #FUNCTION# ==================================================================================================================== # Name...........: _EnableDHCP_DNS # Description....: Enables the Dynamic Host Configuration Protocol (DHCP) for DNS configuration. # Syntax.........: _EnableDHCP_DNS($sNetAdapter) # Parameters.....: $sNetAdapter - Name of the network adapter. # The Windows network connection name can be used instead of network adapter. # Return values..: Success - 1 # Failure - 0 # =============================================================================================================================== def EnableDHCP_DNS(NetAdapter): wmi_instance = win32com.client.GetObject(r"winmgmts:\\"+ Name_PC + r"\root\cimv2") if wmi_instance == 0: return 0 adapterName = GetNetworkAdapterFromID(NetAdapter) if adapterName: NetAdapter = adapterName Guid = str(GetNetworkGUI(NetAdapter)) if Guid == 0: return 0 QueryNetAdapterConfig = str("select * from Win32_NetworkAdapterConfiguration Where Caption like '%" + NetAdapter + "'") colNetAdapterConfig = wmi_instance.ExecQuery(QueryNetAdapterConfig,"WQL",(wbemFlagReturnImmediately+wbemFlagForwardOnly)) if not type(colNetAdapterConfig) is win32com.client.CDispatch: return 0 for objNetAdapter in colNetAdapterConfig: try: return_obj = objNetAdapter.EnableDHCP() if return_obj == False: return 0 except: pass Reg = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE) key = winreg.OpenKey(Reg,r"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"+ Guid.lower()) Result = winreg.SetValue(key,"NameServer", winreg.REG_SZ,"") return Result pprint(DisableNetAdapter("Ethernet")) #pprint(EnableNetAdapter("Ethernet"))