markrogoyski/ipv4-subnet-calculator

子网掩码和其他无类(CIDR)网络信息的网络计算器。

v4.0.0 2021-12-28 23:59 UTC

This package is auto-updated.

Last update: 2024-08-29 03:52:52 UTC


README

子网掩码和其他无类(CIDR)网络信息的网络计算器。

Coverage Status License

功能

给定一个IP地址和CIDR网络大小,它计算网络信息并提供一站式聚合报告。

计算

  • IP地址
  • 网络大小
  • 子网掩码
  • 网络部分
  • 主机部分
  • 网络中的IP地址数量
  • 网络中的可寻址主机数量
  • IP地址范围
  • 广播地址
  • 最小和最大主机
  • 所有IP地址
  • IP地址是否在子网中
  • IPv4 ARPA域

提供点分十进制、十六进制和二进制格式的每个数据,以及四元组数组。

聚合网络计算报告

  • 关联数组
  • JSON
  • 字符串
  • 打印到STDOUT

标准接口

  • JsonSerializable

设置

将库添加到项目中composer.json文件

{
  "require": {
      "markrogoyski/ipv4-subnet-calculator": "4.*"
  }
}

使用 composer 安装库

$ php composer.phar install

Composer将在您的供应商文件夹内安装IPv4 Subnet Calculator。然后您可以将以下内容添加到您的.php文件中,以使用自动加载使用库。

require_once(__DIR__ . '/vendor/autoload.php');

或者,在命令行中使用composer require和安装IPv4 SubnetCalculator

$ php composer.phar require markrogoyski/ipv4-subnet-calculator:4.*

最低要求

  • PHP 7.2

注意:对于PHP 5.5至7.1,请使用v3.0(markrogoyski/ipv4-subnet-calculator:3.*

用法

创建新的SubnetCalculator

// For network 192.168.112.203/23
$sub = new IPv4\SubnetCalculator('192.168.112.203', 23);

各种网络信息

$numbeIpAddresses     = $sub->getNumberIPAddresses();      // 512
$numberHosts          = $sub->getNumberAddressableHosts(); // 510
$addressRange         = $sub->getIPAddressRange();         // [192.168.112.0, 192.168.113.255]
$addressableHostRange = $sub->getAddressableHostRange();   // [192.168.112.1, 192.168.113.254]
$networkSize          = $sub->getNetworkSize();            // 23
$broadcastAddress     = $sub->getBroadcastAddress();       // 192.168.113.255

IP地址

$ipAddress        = $sub->getIPAddress();        // 192.168.112.203
$ipAddressQuads   = $sub->getIPAddressQuads();   // [192, 168, 112, 203]
$ipAddressHex     = $sub->getIPAddressHex();     // C0A870CB
$ipAddressBinary  = $sub->getIPAddressBinary();  // 11000000101010000111000011001011
$ipAddressInteger = $sub->getIPAddressInteger(); // 3232264395;

子网掩码

$subnetMask        = $sub->getSubnetMask();        // 255.255.254.0
$subnetMaskQuads   = $sub->getSubnetMaskQuads();   // [255, 255, 254, 0]
$subnetMaskHex     = $sub->getSubnetMaskHex();     // FFFFFE00
$subnetMaskBinary  = $sub->getSubnetMaskBinary();  // 11111111111111111111111000000000
$subnetMaskInteger = $sub->getSubnetMaskInteger(); // 4294966784

网络部分

$network        = $sub->getNetworkPortion();        // 192.168.112.0
$networkQuads   = $sub->getNetworkPortionQuads();   // [192, 168, 112, 0]
$networkHex     = $sub->getNetworkPortionHex();     // C0A87000
$networkBinary  = $sub->getNetworkPortionBinary();  // 11000000101010000111000000000000
$networkInteger = $sub->getNetworkPortionInteger(); // 3232264192

主机部分

$host        = $sub->getHostPortion();        // 0.0.0.203
$hostQuads   = $sub->getHostPortionQuads();   // [0, 0, 0, 203]
$hostHex     = $sub->getHostPortionHex();     // 000000CB
$hostBinary  = $sub->getHostPortionBinary();  // 00000000000000000000000011001011
$hostInteger = $sub->getHostPortionInteger(); // 203

最小和最大主机

$minHost        = $sub->getMinHost();        // 192.168.112.1
$minHostQuads   = $sub->getMinHostQuads();   // [192, 168, 112, 1]
$minHostHex     = $sub->getMinHostHex();     // C0A87001
$minHostBinary  = $sub->getMinHostBinary();  // 11000000101010000111000000000001
$minHostInteger = $sub->getMinHostInteger(); // 3232264193

$maxHost        = $sub->getMaxHost();        // 192.168.113.254
$maxHostQuads   = $sub->getMaxHostQuads();   // [192, 168, 113, 254]
$maxHostHex     = $sub->getMaxHostHex();     // C0A871FE
$maxHostBinary  = $sub->getMaxHostBinary();  // 11000000101010000111000111111110
$maxHostInteger = $sub->getMaxHostInteger(); // 3232264702

所有IP地址

foreach ($sub->getAllIPAddresses() as $ipAddress) {
    echo $ipAddress;
}

foreach ($sub->getAllHostIPAddresses() as $hostAddress) {
    echo $hostAddress;
}

IP地址是否在子网中

$boolTrue  = $sub->isIPAddressInSubnet('192.168.112.5');
$boolFalse = $sub->isIPAddressInSubnet('192.168.111.5');

反向DNS查找(ARPA域)

$ipv4ArpaDomain = $sub->getIPv4ArpaDomain(); // 203.112.168.192.in-addr.arpa

报告

打印报告

$sub->printSubnetReport();
/*
192.168.112.203/23           Quads      Hex                           Binary    Integer
------------------ --------------- -------- -------------------------------- ----------
IP Address:        192.168.112.203 C0A870CB 11000000101010000111000011001011 3232264395
Subnet Mask:         255.255.254.0 FFFFFE00 11111111111111111111111000000000 4294966784
Network Portion:     192.168.112.0 C0A87000 11000000101010000111000000000000 3232264192
Host Portion:            0.0.0.203 000000CB 00000000000000000000000011001011        203

Number of IP Addresses:      512
Number of Addressable Hosts: 510
IP Address Range:            192.168.112.0 - 192.168.113.255
Broadcast Address:           192.168.113.255
Min Host:                    192.168.112.1
Max Host:                    192.168.113.254
IPv4 ARPA Domain:            203.112.168.192.in-addr.arpa
*/

数组报告

$sub->getSubnetArrayReport();
/*
(
    [ip_address_with_network_size] => 192.168.112.203/23
    [ip_address] => Array
        (
            [quads] => 192.168.112.203
            [hex] => C0A870CB
            [binary] => 11000000101010000111000011001011
            [integer] => 3232264395
        )

    [subnet_mask] => Array
        (
            [quads] => 255.255.254.0
            [hex] => FFFFFE00
            [binary] => 11111111111111111111111000000000
            [integer] => 4294966784
        )

    [network_portion] => Array
        (
            [quads] => 192.168.112.0
            [hex] => C0A87000
            [binary] => 11000000101010000111000000000000
            [integer] => 3232264192
        )

    [host_portion] => Array
        (
            [quads] => 0.0.0.203
            [hex] => 000000CB
            [binary] => 00000000000000000000000011001011
            [integer] => 203
        )

    [network_size] => 23
    [number_of_ip_addresses] => 512
    [number_of_addressable_hosts] => 510
    [ip_address_range] => Array
        (
            [0] => 192.168.112.0
            [1] => 192.168.113.255
        )

    [broadcast_address] => 192.168.113.255
    [min_host] => 192.168.112.1
    [max_host] => 192.168.113.254
    [ipv4_arpa_domain] => 203.112.168.192.in-addr.arpa
)
*/

JSON报告

$sub->getSubnetJSONReport();
/*
{
    "ip_address_with_network_size": "192.168.112.203\/23",
    "ip_address": {
        "quads": "192.168.112.203",
        "hex": "C0A870CB",
        "binary": "11000000101010000111000011001011",
        "integer": 3232264395
    },
    "subnet_mask": {
        "quads": "255.255.254.0",
        "hex": "FFFFFE00",
        "binary": "11111111111111111111111000000000",
        "integer": 4294966784
    },
    "network_portion": {
        "quads": "192.168.112.0",
        "hex": "C0A87000",
        "binary": "11000000101010000111000000000000",
        "integer": 3232264192
    },
    "host_portion": {
        "quads": "0.0.0.203",
        "hex": "000000CB",
        "binary": "00000000000000000000000011001011",
        "integer": 203
    },
    "network_size": 23,
    "number_of_ip_addresses": 512,
    "number_of_addressable_hosts": 510,
    "ip_address_range": [
        "192.168.112.0",
        "192.168.113.255"
    ],
    "broadcast_address": "192.168.113.255",
    "min_host": "192.168.112.1",
    "max_host": "192.168.113.254",
    "ipv4_arpa_domain": "203.112.168.192.in-addr.arpa"
}
*/

字符串报告

$stringReport = $sub->getPrintableReport();
/*
192.168.112.203/23           Quads      Hex                           Binary    Integer
------------------ --------------- -------- -------------------------------- ----------
IP Address:        192.168.112.203 C0A870CB 11000000101010000111000011001011 3232264395
Subnet Mask:         255.255.254.0 FFFFFE00 11111111111111111111111000000000 4294966784
Network Portion:     192.168.112.0 C0A87000 11000000101010000111000000000000 3232264192
Host Portion:            0.0.0.203 000000CB 00000000000000000000000011001011        203

Number of IP Addresses:      512
Number of Addressable Hosts: 510
IP Address Range:            192.168.112.0 - 192.168.113.255
Broadcast Address:           192.168.113.255
Min Host:                    192.168.112.1
Max Host:                    192.168.113.254
IPv4 ARPA Domain:            203.112.168.192.in-addr.arpa
*/

打印 - 字符串表示

print($sub);
/*
192.168.112.203/23           Quads      Hex                           Binary    Integer
------------------ --------------- -------- -------------------------------- ----------
IP Address:        192.168.112.203 C0A870CB 11000000101010000111000011001011 3232264395
Subnet Mask:         255.255.254.0 FFFFFE00 11111111111111111111111000000000 4294966784
Network Portion:     192.168.112.0 C0A87000 11000000101010000111000000000000 3232264192
Host Portion:            0.0.0.203 000000CB 00000000000000000000000011001011        203

Number of IP Addresses:      512
Number of Addressable Hosts: 510
IP Address Range:            192.168.112.0 - 192.168.113.255
Broadcast Address:           192.168.113.255
Min Host:                    192.168.112.1
Max Host:                    192.168.113.254
IPv4 ARPA Domain:            203.112.168.192.in-addr.arpa
*/

标准接口

JsonSerializable

$json = \json_encode($sub);

单元测试

$ cd tests
$ phpunit

Coverage Status

标准

IPv4 Subnet Calculator (PHP) 符合以下标准

许可证

IPv4 Subnet Calculator (PHP) 在MIT许可证下授权。