1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| 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...........: _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...........: _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))
pprint(DisableNetAdapter("Ethernet")) |
Partager