1.1.2 • Published 4 years ago

ip-regex-generate v1.1.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
4 years ago

ip-regex-generator

Generate valid subnet ip regex string by ip in netmask.

Install

$ npm install ip-regex-generator

Usage

const { getSubnetMaskRegex } = require('ip-regex-generator');

getSubnetMaskRegex('192.168.0.1', 24);
getSubnetMaskRegex('192.168.0.1', '24');
//=> ^192\.168\.0\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

getSubnetMaskRegex('192.168.0.1', '26');
//=> ^192\.168\.1\.([0]?[0]?[0-9]|[0]?[1-5][0-9]|[0]?[1-6][0-3])$

getSubnetMaskRegex('192.168.0.1', '8');
//=> ^192\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

getSubnetMaskRegex('192.168.0', '8'); // incomplete
getSubnetMaskRegex('192.168.0.*', '8'); // not support
//=> null

Function

getIPArray(String ip)

Get IP Array from IP String. If ip is invalid return null.

getIPArray('192.168.0.1');
//=> ["192","168","0","1"]

getIPArray('192.168.');
//=> null

getSubnetMaskString(Number netmask)

*netmask value allow 0-32 Get IP type netmask String from netmask string If netmask invalid return null.

getSubnetMaskString('24');
//=> 255.255.255.0

getSubnetMaskString('8');
//=> 255.0.0.0

getSubnetMaskString('33');
//=> null

getSubnetMaskNum(String ip)

Get subnetmask Number by network. If network not have netmask number or number is invalid return null.

getSubnetMaskNum('192.168.0.1/24');
//=> 24

getSubnetMaskNum('192.168.0.1');
//=> null

getSubnetMaskNum('192.168.0.1/33');
//=> null

isIPinMask(String targetIP, String ipWithMask)

Check targetIP is in ipWithMask network. If targetIP or ipWithMask is invalid return false.

isIPinMask('192.168.0.254','192.168.0.1/24');
//=> true

isIPinMask('192.168.1.1','192.168.0.1/24');
isIPinMask('10.10.10.10', '192.168.0.1/24');
isIPinMask('192.168.0', '192.168.0.1/24');
//=> false

getSubnetType(String ip)

Get subnet type(Public IP or Private IP) by ip. If ip is invalid return null. If ip is 127.0.0.1 return localhost.

getSubnetType('127.0.0.1');
//=> localhost

getSubnetType('8.8.8.8');
//=> Public IP

getSubnetType('10.1.1.1');
getSubnetType('172.16.1.1');
getSubnetType('192.168.1.1');
//=> Private IP

getIPClass(String ip)

Get subnet class(A, B, C, D, E) by ip. If ip is invalid return null.

getIPClass('8.8.8.8');
//=> A

getIPClass('192.168.0.1');
//=> C

getIPClass('192.168')
getIPClass('300.300.0.1')
//=> null

getIPInfo(String ip, Number netmask)

*netmask is optional Get IP Information.

getIPInfo('192.168.0.1','24')
//=> IP Address: 192.168.0.1
//   IP Class: C
//   Netmask: 255.255.255.0
//   Subnet Type: Private IP

getIPInfo('192.168.0.1')
//=> IP Address: 192.168.0.1
//   IP Class: C
//   Subnet Type: Private IP

getIPInfo('192.168.0', '24')
// IP Address: invalid
// Netmask: 255.255.255.0
// IP Class: Unknown
// Subnet Type: Unknown

License

MIT © Rong Wu

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago