Skip to content

Instantly share code, notes, and snippets.

@ta1hia
Created July 27, 2015 20:45
Show Gist options
  • Save ta1hia/f36df3ddde850531a693 to your computer and use it in GitHub Desktop.
Save ta1hia/f36df3ddde850531a693 to your computer and use it in GitHub Desktop.
rough script, still in development
package main
import (
"fmt"
"math"
"strconv"
"strings"
)
func IPToInt(ip string) uint {
octets := strings.Split(ip, ".")
ipint := float64(0)
for i := 0; i < 4; i++ {
octet, _ := strconv.ParseFloat(octets[i], 64)
ipint += octet * math.Pow(float64(256), float64(3-i))
}
return uint(ipint)
}
func IntToIP(ipint int) string {
return fmt.Sprintf("%d.%d.%d.%d", ipint>>24, ipint>>16&0xFF, ipint>>8&0xFF, ipint&0xFF)
}
func IPRangeToCIDR(startip, endip uint) {
bits := uint(1)
mask := uint(1)
newip := uint(0)
for bits < 32 {
newip = startip | mask
if newip > endip || ((startip>>bits)<<bits) != startip {
bits = bits - 1
mask = mask >> 1
break
}
bits = bits + 1
mask = (mask << 1) + 1
}
newip = startip | mask
bits = 32 - bits
fmt.Printf("%s/%d\n", IntToIP(int(startip)), bits)
if newip < endip {
IPRangeToCIDR(newip+1, endip)
}
}
func main() {
fmt.Println("iprange to cidr")
fmt.Println(IPToInt("66.207.219.190"))
fmt.Println(IntToIP(1120918462))
IPRangeToCIDR(IPToInt("221.192.0.0"), IPToInt("221.199.207.255"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment