Win32::Net::Info - Win32 Network Adapter Interface
use Win32::Net::Info; my $interface = Win32::Net::Info->new('Wireless Network Connection'); printf "Name: %s\n", $interface->name; printf "MAC: %s\n", $interface->mac; printf "IPv4: %s\n", $interface->ipv4;
Win32::Net::Info is a module to retrieve Windows interface adapter information (such as IP[v6] addresses, gateways, MAC, etc...). It is implemented with system functions such as wmic, netsh and arp. A better approach may be to use XS with GetAdaptersAddresses() and parse the IP_ADAPTER_ADDRESSES structure. Alas, that is proving difficult to do.
wmic
netsh
arp
GetAdaptersAddresses()
IP_ADAPTER_ADDRESSES
This module was developed since I couldn't find an existing CPAN module that handled this information specifically for Windows, let alone find Win32 support in the many interface modules already on CPAN (see SEE ALSO). The existing CPAN interface modules also used different APIs so finding a common interface for both Windows and *nix with all the features was not possible.
This modules attempts to provide many of the API calls from *nix interface modules specifically to Win32.
my $interfaces = Win32::Net::Info->new([OPTIONS]);
Create a new Win32::Net::Info object with OPTIONS as optional parameters. Valid options are:
Option Description Default ------ ----------- ------- -interface Friendly name of interface Local Area Connection -dev -device
Called with option eliminates the need to call the interface() method.
Single option indicates interface.
my $interface = $interfaces->interface(OPTIONS);
Find provided interface and populate the return object. Valid options are:
Option Description Default ------ ----------- ------- -interface Friendly name of interface [none] -dev -device
Allows the following to be called.
$interface->name();
Return friendly name of interface. This in in the form of 'Local Area Connection', for example.
$interface->adaptername();
Return adapter name of interface. This is in the form of '{1234ABCD-12AB-34CD-56EF-123456ABCDEF}', for example.
$interface->hostname();
Return host name of interface.
Alias:
$interface->domainname();
Return domain name of interface.
$interface->devicename();
Return adapter name of interface. This is in the form of '\Device\NPF_{1234ABCD-12AB-34CD-56EF-123456ABCDEF}', for example. Optional argument drops the PCap prefix (default '\Device\NPF_').
$interface->description();
Return description of interface.
$interface->ifindex();
Return interface index of interface.
$interface->mac();
Return MAC address of interface.
[$ret | @ret =] $interface->ipv4();
Return IPv4 address of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv4_netmask();
Return IPv4 network mask of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6();
Return IPv6 address of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6_netmask();
Return IPv6 network mask of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6_link_local();
Return IPv6 link-local address of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6_link_local_netmask();
Return IPv6 link local network mask of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv4_default_gateway();
Return IPv4 default gateway of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv4_gateway_metric();
Return IPv4 gateway metric of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6_default_gateway();
Return IPv6 default gateway of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6_gateway_metric();
Return IPv6 gateway metric of interface. Scalar context returns first, array context returns all.
$interface->ipv4_gateway_mac();
Return MAC address of IPv4 default gateway of interface.
$interface->ipv6_gateway_mac();
Return MAC address of IPv6 default gateway of interface.
$interface->ipv4_mtu();
Return MTU of interface for IPv4.
$interface->ipv6_mtu();
Return MTU of interface for IPv6.
$interface->mtu();
Return MTU of interface. Minimum value between IPv4 and IPv6 MTU if both exist; otherwise, just the value of the MTU that does exist. Undef if neither exist.
[$ret | @ret =] $interface->ipv4_dns_server();
Return IPv4 DNS servers of interface. Scalar context returns first, array context returns all.
[$ret | @ret =] $interface->ipv6_dns_server();
Return IPv6 DNS servers of interface. Scalar context returns first, array context returns all.
$interface->dump;
Dump all interface information (basically, all of the above).
$interface->pcap_prefix([name]);
Return the PCap prefix (default '\Device\NPF_'). Optional name sets the PCap prefix for future calls to adaptername.
printf "Error: %s\n", Win32::Net::Info->error;
Return last error.
my $interfaces = Win32::Net::Info->interfaces;
List available interfaces by friendly name.
Context Usage Return ------- ----- ------ none Win32::Net::Info->interfaces; (print list) SCALAR $i = Win32::Net::Info->interfaces; array ref ARRAY @i = Win32::Net::Info->interfaces; array
my $mac = Win32::Net::Info->lookupMac(ipv4_addr);
Return MAC address for provided IPv4 address.
my $mac = Win32::Net::Info->lookupMac6(ipv6_addr);
Return MAC address for provided IPv6 address.
Load them: use Win32::Net::Info qw(:subs):
use Win32::Net::Info qw(:subs)
IO::Interface, Net::Interface, Win32::IPHelper, Win32::IPConfig, Net::Libdnet
This software is released under the same terms as Perl itself. If you don't know what that means visit http://perl.com/.
Copyright (c) 2011 Michael Vincent
http://www.VinsWorld.com
All rights reserved
To install Win32::Net::Info, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::Net::Info
CPAN shell
perl -MCPAN -e shell install Win32::Net::Info
For more information on module installation, please visit the detailed CPAN module installation guide.