detain / ip-tools

适用于IPv4和IPv6的通用IP工具。用于子网掩码和其他无类别(CIDR)网络信息的网络计算器。

此软件包的官方仓库似乎已消失,因此软件包已被冻结。

v2.0.0 2018-06-05 04:17 UTC

This package is auto-updated.

Last update: 2019-11-06 18:54:26 UTC


README

通用网络计算器和IPv4地址、IPv6地址的IP工具,用于子网掩码操作和获取(CIDR)网络信息。

Build Status Latest Stable Version Total Downloads Downloads Month License Coverage Status

功能

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

计算

  • IP地址
  • 网络大小
  • 子网掩码
  • 网络部分
  • 主机部分
  • 网络中的IP地址数量
  • 网络中可寻址的主机数量
  • IP地址范围
  • 广播地址
  • 最小和最大主机

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

综合网络计算报告

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

设置

最低要求

  • PHP 5.3.0

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

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

使用 composer 安装库

$ php composer.phar install

Composer将在您的vendor文件夹内安装IPv4子网计算器。然后您可以在.php文件中添加以下内容以使用自动加载使用库。

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

使用Composer要求此软件包

通过 Composer 安装此软件包。编辑您项目中的 composer.json 文件以要求 detain/ip-tools

创建 composer.json 文件

{
	"name": "yourproject/yourproject",
	"type": "project",
	"require": {
		"detain/ip-tools": "~1.1.0"
	}
}

然后运行 composer update

或者 在命令行中运行命令

composer require detain/ip-tools

用法

<?php
$loader = require __DIR__.'/vendor/autoload.php';

use Detain\IPTools\Ip;

// Validating
$status = Ip::isValid('192.168.1.1'); // true

$status = Ip::isValid('192.168.1.256'); // false


// ip2long, long2ip

/// IPv4
$long = Ip::ip2long('192.168.1.1'); // 3232235777

$dec = Ip::long2ip('3232235777'); // 192.168.1.1

/// IPv6
$long = Ip::ip2long('fe80:0:0:0:202:b3ff:fe1e:8329'); // 338288524927261089654163772891438416681

$dec = Ip::long2ip('338288524927261089654163772891438416681', true); // fe80::202:b3ff:fe1e:8329


// Matching

/// IPv4
$status = Ip::match('192.168.1.1', '192.168.1.*'); // true

$status = Ip::match('192.168.1.1', '192.168.*.*'); // true

$status = Ip::match('192.168.1.1', '192.168.*.*'); // true

$status = Ip::match('192.168.1.1', '192.168.0.*'); // false


$status = Ip::match('192.168.1.1', '192.168.1/24'); // true

$status = Ip::match('192.168.1.1', '192.168.1.1/255.255.255.0'); // true

$status = Ip::match('192.168.1.1', '192.168.0/24'); // false

$status = Ip::match('192.168.1.1', '192.168.0.0/255.255.255.0'); // false


$status = Ip::match('192.168.1.5', '192.168.1.1-192.168.1.10'); // true

$status = Ip::match('192.168.5.5', '192.168.1.1-192.168.10.10'); // true

$status = Ip::match('192.168.5.5', '192.168.6.1-192.168.6.10');


$status = Ip::match('192.168.1.1', array('122.128.123.123', '192.168.1.*', '192.168.123.124')); // true

$status = Ip::match('192.168.1.1', array('192.168.123.*', '192.168.123.124'));

/// IPv6

$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:9652', '2001:cdba:0000:0000:0000:0000:3257:*'); // true

$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:9652', '2001:cdba:0000:0000:0000:0000:*:*'); // true

$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:9652',
					'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:9999'); // true


$status = Ip::match('2001:cdba:0000:0000:0000:0000:3258:9652', '2001:cdba:0000:0000:0000:0000:3257:*'); // false

$status = Ip::match('2001:cdba:0000:0000:0000:1234:3258:9652', '2001:cdba:0000:0000:0000:0000:*:*'); // false

$status = Ip::match('2001:cdba:0000:0000:0000:0000:3257:7778',
					'2001:cdba:0000:0000:0000:0000:3257:1234-2001:cdba:0000:0000:0000:0000:3257:7777'); // false

用法

创建新子网

// For network 192.168.112.203/23
$sub = new Net\Subnet('192.168.112.203', 23);

各种网络信息

$number_ip_addresses    = $sub->getNumberIPAddresses();      // 512
$number_hosts           = $sub->getNumberAddressableHosts(); // 510
$address_rage           = $sub->getIPAddressRange();         // [192.168.112.0, 192.168.113.255]
$addressable_host_range = $sub->getAddressableHostRange();   // [192.168.112.1, 192.168.113.254]
$network_size           = $sub->getNetworkSize();            // 23
$broadcast_address      = $sub->getBroadcastAddress();       // 192.168.113.255

IP地址

$ip_address        = $sub->getIPAddress();       // 192.168.112.203
$ip_address_quads  = $sub->getIPAddressQuads();  // [192, 168, 112, 203]
$ip_address_hex    = $sub->getIPAddressHex();    // C0A870CB
$ip_address_binary = $sub->getIPAddressBinary(); // 11000000101010000111000011001011

子网掩码

$subnet_mask        = $sub->getSubnetMask();       // 255.255.254.0
$subnet_mask_quads  = $sub->getSubnetMaskQuads();  // [255, 255, 254, 0]
$subnet_mask_hex    = $sub->getSubnetMaskHex();    // FFFFFE00
$subnet_mask_binary = $sub->getSubnetMaskBinary(); // 11111111111111111111111000000000

网络部分

$network        = $sub->getNetworkPortion();       // 192.168.112.0
$network_quads  = $sub->getNetworkPortionQuads();  // [192, 168, 112, 0]
$network_hex    = $sub->getNetworkPortionHex();    // C0A87000
$network_binary = $sub->getNetworkPortionBinary(); // 11000000101010000111000000000000

主机部分

$host        = $sub->getHostPortion();       // 0.0.0.203
$host_quads  = $sub->getHostPortionQuads();  // [0, 0, 0, 203]
$host_hex    = $sub->getHostPortionHex();    // 000000CB
$host_binary = $sub->getHostPortionBinary(); // 00000000000000000000000011001011

最小和最大主机

$min_host        = $sub->getMinHost();       // 192.168.112.1
$min_host_quads  = $sub->getMinHostQuads();  // [192, 168, 112, 1]
$min_host_hex    = $sub->getMinHostHex();    // C0A87001
$min_host_binary = $sub->getMinHostBinary(); // 11000000101010000111000000000001

$max_host        = $sub->getMaxHost();       // 192.168.113.254
$max_host_quads  = $sub->getMaxHostQuads();  // [192, 168, 113, 254]
$max_host_hex    = $sub->getMaxHostHex();    // C0A871FE
$max_host_binary = $sub->getMaxHostBinary(); // 11000000101010000111000111111110

报告

打印报告

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

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
*/

数组报告

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

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

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

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

	[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
)
*/

JSON报告

$sub->getJSONReport();
/*
{
	"ip_address_with_network_size": "192.168.112.203/23",
	"ip_address": {
		"quads": "192.168.112.203",
		"hex": "C0A870CB",
		"binary": "11000000101010000111000011001011"
	},
	"subnet_mask": {
		"quads": "255.255.254.0",
		"hex": "FFFFFE00",
		"binary": "11111111111111111111111000000000"
	},
	"network_portion": {
		"quads": "192.168.112.0",
		"hex": "C0A87000",
		"binary": "11000000101010000111000000000000"
	},
	"host_portion": {
		"quads": "0.0.0.203",
		"hex": "000000CB",
		"binary": "00000000000000000000000011001011"
	},
	"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"
}
*/

字符串报告

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

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
*/

// Printing the Subnet object will print the printable report.
print($sub);
/*
192.168.112.203/23           Quads      Hex                           Binary
------------------ --------------- -------- --------------------------------
IP Address:        192.168.112.203 C0A870CB 11000000101010000111000011001011
Subnet Mask:         255.255.254.0 FFFFFE00 11111111111111111111111000000000
Network Portion:     192.168.112.0 C0A87000 11000000101010000111000000000000
Host Portion:            0.0.0.203 000000CB 00000000000000000000000011001011

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
*/

单元测试

$ cd tests
$ phpunit

Coverage Status

标准

IPv4子网计算器(PHP)符合以下标准

此代码可在Github上获取。欢迎提交拉取请求。

故障排除

如果您喜欢走在前沿,请将您发现的任何错误报告到PHP IP Tools问题页面。

贡献

有关信息,请参阅CONTRIBUTING.md

许可证

请参阅此存储库中包含的LICENSE,以获取MIT许可证的完整副本,该项目根据此许可证授权。

鸣谢