Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How can I get current IP,DNS,MAC,GW under XP?

by perllee (Novice)
on Nov 20, 2008 at 07:42 UTC ( #724812=perlquestion: print w/ replies, xml ) Need Help??
perllee has asked for the wisdom of the Perl Monks concerning the following question:

Hi,some friends:

I want to write a simple Network check tool with perl,for my colleagues. Ping 127.0.0.1,ping default gateway(GW),ping DNS,etc. Preliminary judge a network failure. If I have two or more NICs, Now how can I get current IP,DNS,MAC and GW under XP?

Thanks!

--perllee, from WU HAN.

Comment on How can I get current IP,DNS,MAC,GW under XP?
Re: How can I get current IP,DNS,MAC,GW?
by Anonymous Monk on Nov 20, 2008 at 07:59 UTC
    shell out ping/ifconfig/ipconfig...
      Thanks, but I think analyse `ipconfig/all` is not a good way. We have a better alternative to handle? I need current IP,MAC,DNS being used. Thanks a lot.

      --perllee, WU HAN.

Re: How can I get current IP,DNS,MAC,GW under XP?
by BrowserUk (Pope) on Nov 20, 2008 at 09:16 UTC

    All of this information is available through apis in various system DLLs. It can also be discovered by examining various keys in the registry. I don't know of any existing module (or combination of modules) that exposes it directly.

    Certainly one could be written, using Win32::API or Inline::C or XS to access some combination of the following entrypoints:

    netman.dll 77D0B3FC 5 HrPnpInstanceIdFromGuid 77D0177D 4 HrLanConnectionNameFromGuidOrPath DNSAPI.dll 76F329BE 21 DnsFreeConfigStructure 76F22D94 4B DnsQuery_W 76F25AD3 50 DnsRecordListFree 76F3757A 1C DnsFlushResolverCache 76F27A1D 44 DnsQueryConfigAllocEx 76F32E94 23 DnsGetCacheDataTable WS2_32.dll 71AB6979 7 WSAAddressToStringW 71AB2EE1 Ordinal 11 71AB2EAD Ordinal 8 71AB45C1 Ordinal 12 71AB3CCE Ordinal 111 71AB6A55 Ordinal 115 iphlpapi.dll 76D6D4F0 6E NhGetInterfaceNameFromGuid 76D62841 2D GetInterfaceInfo 76D65143 2A GetIfTable 76D63B9C 2E GetIpAddrTable 76D6AAC7 31 GetIpForwardTable 76D66AE8 6F NhpAllocateAndGetInterfaceInfoFromStack 76D6CCCF 39 GetNetworkParams 76D69B93 35 GetIpStatistics 76D63E54 1A GetAdaptersAddresses SETUPAPI.dll 7792C341 10E SetupDiCreateDeviceInfoList 7792EEEC 166 SetupDiOpenDeviceInfoW 7792B28D 11B SetupDiDestroyDeviceInfoList 7792EB01 146 SetupDiGetDeviceRegistryPropertyW DHCPCSVC.DLL 7D4BFD79 23 DhcpStaticRefreshParams 7D4C1F11 D DhcpHandlePnPEvent 7D4C1CA3 8 DhcpEnumClasses 7D4BFBC1 1B DhcpReleaseParameters 7D4BFA6F 0 DhcpAcquireParameters

    But since that's just a selective imports dump from ipconfig.exe, which already knows how to get this information (and will be maintained by MS through patches and upgrades), and the output format of ipconfig /all is relatively straight forward to parse. The latter approach seems like a far simpler, quicker, and more maintainable way to go.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How can I get current IP,DNS,MAC,GW under XP?
by oshalla (Deacon) on Nov 20, 2008 at 10:27 UTC

    Have you considered Win32::IPHelper ? (I haven't used it, but it looks plausible enough. There may be other Win32:: modules that will also do the job ? CPAN is your friend.)

Re: How can I get current IP,DNS,MAC,GW under XP?
by idsfa (Vicar) on Nov 20, 2008 at 14:36 UTC
    Net::Interface and Net::DNS::Resolver can provide those answers.

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: How can I get current IP,DNS,MAC,GW under XP?
by bingos (Vicar) on Nov 20, 2008 at 15:00 UTC

    You can use WMI to query this type of information.

    Consult the documentation for Win32_NetworkAdapterConfiguration

    Here's something I very quickly threw together:

    use strict; use warnings; use Win32::OLE qw(in); $|++; my $object = Win32::OLE->GetObject('winmgmts:{impersonationLevel=imper +sonate}!//' . '.' ); foreach my $nic ( in $object->InstancesOf('Win32_NetworkAdapterConfigu +ration') ) { next unless $nic->{IPEnabled}; print "Adapter: "; print $nic->{Caption}, "\n"; print "Default gateway: ", join(' ', @{ $nic->{DefaultIPGateway} }) +, "\n" if $nic->{DefaultIPGateway}; my %addresses; @addresses{ @{ $nic->{IPAddress} } } = @{ $nic->{IPSubnet} }; print "IP Addresses: \n"; print "IP: $_ Mask: ", $addresses{$_}, "\n" for keys %addresses; print "DNS Servers: ", join(' ', @{ $nic->{DNSServerSearchOrder} } +), "\n" if $nic->{DNSServerSearchOrder}; } exit 0;
      First of all,thank you, above all enthusiastic response. But I think I do not have a clear description of the question. I do not need have a list of IP,DNS,etc on all NICs. In fact, I would like to know which NIC is working,then get the IP,DNS,MAC,etc on that NIC.

      For example, c:>ipconfig/all

      Windows IP Configuration Host Name . . . . . . . . . . . . : perl Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Unknown IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No Ethernet adapter VMware Network Adapter VMnet8: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : VMware Virtual Ethernet Ad +apter for VMnet8 Physical Address. . . . . . . . . : 00-50-56-C0-11-08 Dhcp Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.168.227.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : Ethernet adapter VMware Network Adapter VMnet1: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : VMware Virtual Ethernet Ad +apter for VMnet1 Physical Address. . . . . . . . . : 00-50-56-C0-11-01 Dhcp Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.168.138.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : Ethernet adapter 无线网络连接: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Broadcom 802.11b/g WLAN Physical Address. . . . . . . . . : 00-1A-73-2A-E6-BC Dhcp Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 192.168.0.103 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.0.1 DHCP Server . . . . . . . . . . . : 192.168.0.1 DNS Servers . . . . . . . . . . . : 192.168.0.1 Lease Obtained. . . . . . . . . . : 2008年11月20日 23:35:02 Lease Expires . . . . . . . . . . : 2008年11月27日 23:35:02 Ethernet adapter 本地连接: Media State . . . . . . . . . . . : Media disconnected Description . . . . . . . . . . . : Broadcom NetLink (TM) Giga +bit Ethernet Physical Address. . . . . . . . . : 00-17-A4-DD-F3-26
      Now, I'm using a wireless network, so I hope the perl script will Automatically get the following:
      Physical Address. . . . . . . . . : 00-1A-73-2A-E6-BC IP Address. . . . . . . . . . . . : 192.168.0.103 Default Gateway . . . . . . . . . : 192.168.0.1 DHCP Server . . . . . . . . . . . : 192.168.0.1 DNS Servers . . . . . . . . . . . : 192.168.0.1
      Poor English skills, understanding.

      Thanks.

        Oh well, I'm feeling generous:

        use strict; use warnings; use Win32::OLE qw(in); $|++; my $machine = '.'; # Replace this to query a remote machine '.' picks +the localhost my $object = Win32::OLE->GetObject('winmgmts:{impersonationLevel=imper +sonate}!//' . $machine ); foreach my $nic ( in $object->InstancesOf('Win32_NetworkAdapterConfigu +ration') ) { next unless $nic->{IPEnabled}; next if $nic->{Caption} =~ /VMware Virtual/; # Mac Address print $nic->{MACAddress}, "\n"; # IP Address(es), there can be more than one print join( ' ', @{ $nic->{IPAddress} } ), "\n"; # Default gateway print join(' ', @{ $nic->{DefaultIPGateway} }), "\n" if $nic->{Defa +ultIPGateway}; # DHCP Server ( if applicable ) print $nic->{DHCPServer}, "\n" if $nic->{DHCPEnabled}; # DNS Servers ( if applicable ) print join(' ', @{ $nic->{DNSServerSearchOrder} } ), "\n" if $nic-> +{DNSServerSearchOrder}; } exit 0;

        Produces something like:

        00:11:2F:41:E5:CE 10.0.127.254 10.4.0.3 10.1.0.7 10.1.0.8 10.1.0.7

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://724812]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (15)
As of 2014-07-29 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls