IPAddress(3pm)
π NAME π
okay so this page is IPAddress(3pm), and it lives in the User Contributed Perl Documentation section β it's the manual for Net::IPAddress, a lil collection of functions used to manipulate IP addresses, masks and FQDN's. slay. β¨
πΈ SYNOPSIS πΈ
use Net::IPAddress;
@ISA = qw(Net::IPAddress);
π¦ DESCRIPTION π¦
bestie. Net::IPAddr is literally just a collection of helpful functions used to convert IP addresses to/from 32-bit integers, apply subnet masks to IP addresses, validate IP address strings, and split a FQDN into its host and domain parts. π«Ά
No rocket science here, no cap β but the author is obsessed because these functions are very, very handy. For example: have you ever tried to sort a list of IP addresses only to find out that they don't sort the way you expected?? π iconic problem. Here is the solution! If you convert the IP addresses to 32-bit integer addresses, they will sort in correct order. periodt.
π
ip2num( STRING )
Returns the 32-bit integer of the passed IP address string.
$ipnum = ip2num("10.1.1.1");
$ipnum is 167837953.
π
num2ip( INTEGER )
Returns the IP address string of the passed 32-bit IP address.
$IP = num2ip(167837953);
$IP is "10.1.1.1".
π
validaddr( STRING )
Returns true (1) if the IP address string is a valid and properly formatted IP address, and false (0) otherwise. vibe check for IP strings, fr fr. β
β
$valid = validaddr("10.1.2.1"); # returns true
$valid = validaddr("10.1.2."); # returns false!
If you have your own IP address validator, try the last one. Most will incorrectly compute that as a valid address. not the fake-valid jump scare π
π
mask( IPADDRESS, MASK )
Returns the result of binary (IPADDRESS & MASK). IPADDRESS can be either an IP address string or a 32-bit integer address. MASK can be either an IP address string, or the number of bits in the mask. The returned value will be in the same format as the passed IP address β so if you pass an IP address string, then an IP address string is returned; if you pass a 32-bit integer address then a 32-bit integer address is returned. mirroring the format, she's consistent. πͺ
Examples:
$subnet = mask("10.96.3.2",16); # $subnet = "10.96.0.0"
$subnet = mask("10.21.4.22","255.240.0.0");
# $subnet = "10.16.0.0"
$subnet = mask(167837953,"255.255.255.0");
# $subnet = 167837952
This function, when used with the others, is very useful for computing IP addresses. For example, you need to add another server to a subnet that an existing server is on. You want the new server to be the ".17" address of a /24 subnet. This is done easily in the following example:
use Net::IPAddress
$server = "10.8.9.12";
$newserver = num2ip(ip2num(mask($server,24)) + 17);
print "New server IP is $newserver\n";
New server IP is 10.8.9.17
The following code does exactly the same thing. bestie said two roads, one destination. π£οΈ
use Net::IPAddress;
$server = "10.8.9.12";
$newserver = num2ip(mask(ip2num($server),24) + 17);
print "New server IP is $newserver\n";
π
fqdn( FQDN )
This function returns the host and domain of the passed FQDN (fully qualified domain name). splitting the couple up, gently. πβ‘οΈπ
($host,$domain) = fqdn("www.cpan.perl.org");
# $host = "www", $domain = "cpan.perl.org"
π EXPORTS π
Net::IPAddress exports five functions: ip2num, num2ip, validaddr, mask, and fqdn. five of them. the whole girl group on stage. π
π AUTHOR π
Scott Renner <srenner@mandtbank.com>, <srenner@comcast.net>
π COPYRIGHT π
Copyright(c) 2003-2005 Scott Renner. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie!! πβ¨ this was IPAddress(3pm) from the User Contributed Perl Documentation, courtesy of perl v5.36.0, dated 2022-10-13 β mwah, thanks for reading, you're doing amazing sweetie π
π¦π
π¦ where this page comes from
- ubuntu noble: libnet-ipaddress-perl 1.10-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libnet-ipaddress-perl 1.10-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libnet-ipaddress-perl 1.10-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms