Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript NETMASK and CIDR conversion

I was expecting to find hundreds of examples of functions to convert to and from CIDR and NETMASK for javascript, but was unable to find any.

I need to convert to and from CIDR and NETMASKS on a nodejs page which sets and retrieves the IP address for a machine using NETCTL.

Any easy solutions to do this using javascript / nodejs ??

like image 255
crankshaft Avatar asked Jan 17 '26 21:01

crankshaft


1 Answers

Here's one that doesn't check if the netmask is valid:

const netmaskToCidr = n => n
  .split('.')
  .reduce((c, o) => c - Math.log2(256 - +o), 32)
like image 78
pdonias Avatar answered Jan 19 '26 13:01

pdonias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!