gmoises wrote: ↑31 Jan 2023, 17:47
It can be done using WMI.
I made this Script that you can adapt easily
At the end there are 2 lists with all the properties of the objects
Code: Select all
/*
Get the data from the physical network adapters,
skip the virtual ones:
skip Bluetooth, TeamViewer, Oracle, ZeroTier, vEthernet.
For each adapter, get the IP, if it doesn't have an IP,
it means it's not connected.
*/
#Requires AutoHotkey v1.1.36+
For Ix, NIC in Sacar_NICs()
{ For Que, Dato in NIC
Txt .= Que . Tabs(Que) . Dato . "`n"
MsgBox % Txt
Txt := ""
}
Sacar_NICs() {
ColNICs := []
strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")
colItems := objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")._NewEnum
While colItems[objItem]
If objItem.PhysicalAdapter and !InStr(objItem.NetConnectionID, "Bluetooth")
If !InStr(objItem.Manufacturer, "TeamViewer") and !InStr(objItem.Manufacturer, "Oracle")
If !InStr(objItem.NetConnectionID, "ZeroTier") and !InStr(objItem.NetConnectionID, "vEthernet")
{ CadaNIC := []
MAC1 := objItem.MACAddress
CadaNIC["Description"] := objItem.Description
CadaNIC["NetConnectionID"] := objItem.NetConnectionID
CadaNIC["Manufacturer"] := objItem.Manufacturer
CadaNIC["ProductName"] := objItem.ProductName
CadaNIC["AdapterType"] := objItem.AdapterType
CadaNIC["MACAddress"] := objItem.MACAddress
CadaNIC["NetConnectionStatus"] := objItem.NetConnectionStatus
CadaNIC["Speed"] := objItem.Speed
CadaNIC["NetEnabled"] := objItem.NetEnabled
colItems2 := objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")._NewEnum
While colItems2[objItem2]
{ Mac2 := objItem2.MACAddress
If (Mac1 = Mac2)
For Cada, Dato in objItem2.IPAddress
{ ++Ix
CadaNIC["IPAddress-" . Ix] := Cada
}
Ix := 0
}
colItems2 := objItem2 := ""
ColNICs.Push(CadaNIC)
}
Return, ColNICs
}
Tabs(ByRef Txt) {
Largo := StrLen(Txt)
If Largo > 16
Return, "`t"
If Largo > 8
Return, "`t`t"
Return, "`t`t`t"
}
/*
[Dynamic, Provider("CIMWin32"), UUID("{8502C4C0-5FBB-11D2-AAC1-006008C78BC7}"), AMENDMENT]
class Win32_NetworkAdapter : CIM_NetworkAdapter
{
string AdapterType;
uint16 AdapterTypeID;
boolean AutoSense;
uint16 Availability;
string Caption;
uint32 ConfigManagerErrorCode;
boolean ConfigManagerUserConfig;
string CreationClassName;
string Description;
string DeviceID;
boolean ErrorCleared;
string ErrorDescription;
string GUID;
uint32 Index;
datetime InstallDate;
boolean Installed;
uint32 InterfaceIndex;
uint32 LastErrorCode;
string MACAddress;
string Manufacturer;
uint32 MaxNumberControlled;
uint64 MaxSpeed;
string Name;
string NetConnectionID;
uint16 NetConnectionStatus;
boolean NetEnabled;
string NetworkAddresses[];
string PermanentAddress;
boolean PhysicalAdapter;
string PNPDeviceID;
uint16 PowerManagementCapabilities[];
boolean PowerManagementSupported;
string ProductName;
string ServiceName;
uint64 Speed;
string Status;
uint16 StatusInfo;
string SystemCreationClassName;
string SystemName;
datetime TimeOfLastReset;
};
*/
/*
[Dynamic, Provider("CIMWin32"), UUID("{8502C515-5FBB-11D2-AAC1-006008C78BC7}"), AMENDMENT]
class Win32_NetworkAdapterConfiguration : CIM_Setting
{
string Caption;
string Description;
string SettingID;
boolean ArpAlwaysSourceRoute;
boolean ArpUseEtherSNAP;
string DatabasePath;
boolean DeadGWDetectEnabled;
string DefaultIPGateway[];
uint8 DefaultTOS;
uint8 DefaultTTL;
boolean DHCPEnabled;
datetime DHCPLeaseExpires;
datetime DHCPLeaseObtained;
string DHCPServer;
string DNSDomain;
string DNSDomainSuffixSearchOrder[];
boolean DNSEnabledForWINSResolution;
string DNSHostName;
string DNSServerSearchOrder[];
boolean DomainDNSRegistrationEnabled;
uint32 ForwardBufferMemory;
boolean FullDNSRegistrationEnabled;
uint16 GatewayCostMetric[];
uint8 IGMPLevel;
uint32 Index;
uint32 InterfaceIndex;
string IPAddress[];
uint32 IPConnectionMetric;
boolean IPEnabled;
boolean IPFilterSecurityEnabled;
boolean IPPortSecurityEnabled;
string IPSecPermitIPProtocols[];
string IPSecPermitTCPPorts[];
string IPSecPermitUDPPorts[];
string IPSubnet[];
boolean IPUseZeroBroadcast;
string IPXAddress;
boolean IPXEnabled;
uint32 IPXFrameType[];
uint32 IPXMediaType;
string IPXNetworkNumber[];
string IPXVirtualNetNumber;
uint32 KeepAliveInterval;
uint32 KeepAliveTime;
string MACAddress;
uint32 MTU;
uint32 NumForwardPackets;
boolean PMTUBHDetectEnabled;
boolean PMTUDiscoveryEnabled;
string ServiceName;
uint32 TcpipNetbiosOptions;
uint32 TcpMaxConnectRetransmissions;
uint32 TcpMaxDataRetransmissions;
uint32 TcpNumConnections;
boolean TcpUseRFC1122UrgentPointer;
uint16 TcpWindowSize;
boolean WINSEnableLMHostsLookup;
string WINSHostLookupFile;
string WINSPrimaryServer;
string WINSScopeID;
string WINSSecondaryServer;
};
*/
Hello everyone.
I'm a beginner in programming but I'm trying to do my best..
This script is really useful for the work I'm trying to do, but I need some help.
I need to get some additional info of the network adapters like subnet mask and gateway.
I know this info is available in the Win32_NetworkAdapterConfiguration class, but I don't know how to edit the script to get this additional info.
Regarding the Ip address, I need only IPv4 and I managed to get this by adding "if (!InStr(Cada, ":"))" to avid showing IPv6.
One more thing I would need is to put all of the info into variables so I can use it further in the script.
So if someone is willing to help me, I would appreciate it a lot!
Thanks!