On Windows, this piece of code can be useful:
import os import sys import _winreg def main(): adapter_list_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards') adapter_count = _winreg.QueryInfoKey(adapter_list_key)[0] for i in xrange(adapter_count): sub_key_name = _winreg.EnumKey(adapter_list_key, i) adapter_key = _winreg.OpenKey(adapter_list_key, sub_key_name) (adapter_service_name, _) = _winreg.QueryValueEx(adapter_key, "ServiceName") (description, _) = _winreg.QueryValueEx(adapter_key, "Description") adapter_registry_path = os.path.join(r'SYSTEM\ControlSet001\Services', adapter_service_name, "Parameters", "Tcpip") adapter_service_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, adapter_registry_path) (subnet_mask, _) = _winreg.QueryValueEx(adapter_service_key, "SubnetMask") (ip_address, _) = _winreg.QueryValueEx(adapter_service_key, "IpAddress") sys.stdout.write("Name: %s\n" % adapter_service_name) sys.stdout.write("Description: %s\n" % description) sys.stdout.write("SubnetMask: %s\n" % subnet_mask) sys.stdout.write("IpAdress: %s\n" % ip_address) if __name__ == "__main__": main()
Obtain a list of network adapters from the registry key HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
, and then extract additional information about each adapter from the key HKLM\SYSTEM\ControlSet001\Services\{adapter_guid}\Parameters\Tcpip
.
I am testing it on Windows XP with 2 virtual adapters, everything works fine. Should work in 2000, 2003 and Vista too.
source share