tags:

views:

232

answers:

1

Given a CIDR, how can I convert it to a subnet mask.

A: 

Same way you do in any other language

set n 24
set mask [expr {~ 0 << ( 32 - $n )}]
format "%d.%d.%d.%d" [expr {$mask >> 24 & 255}] [expr {$mask >> 16 & 255}] [expr {$mask >> 8 & 255}] [expr {$mask & 255}]
ephemient
Thanks. What does set mask line actually do ?
It creates a mask of "bits" that get shifted around, truly working with the data as a bitmask.
Nerdling
Related to this question, How can I add 1 to an IP address (Example: 192.168.1.0). I have an IP address and a Subnet mask and I am trying to get the first valid IP address in the range. If I do a logical AND between IP and Subnet, I get the subnet number. I need to add 1 to the subnet number to get the first IP.