V
V
Vladimir Grabko2016-04-23 16:43:36
go
Vladimir Grabko, 2016-04-23 16:43:36

How to allocate ip range?

Good afternoon. I have an ip range, for example 127.0.0.1-127.0.225.224. I have 4 machines (that will scan the range) and I need to divide this range into 4 parts. I googled for a long time and didn’t find anything (
At the output, I will get a distributed port scanner. Of course, there may be 2 machines, or even a million)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Grabko, 2016-04-23
@lucifer-m

Thanks Nikita . Having slightly altered your example, I got what I wanted.

package main

import (
  "fmt"
  "net"
)

func inet_aton(ip string) (ip_int uint32) {
  ip_byte := net.ParseIP(ip).To4()
  for i := 0; i < len(ip_byte); i++ {
    ip_int |= uint32(ip_byte[i])
    if i < 3 {
      ip_int <<= 8
    }
  }
  return
}

func inet_ntoa(ip uint32) string {
  return fmt.Sprintf("%d.%d.%d.%d", byte(ip>>24), byte(ip>>16), byte(ip>>8),
    byte(ip))
}


func main() {
  ip0 := inet_aton("127.0.0.1")
  ip1 := inet_aton("127.225.225.224")
  
  
  diff := ip1 - ip0
  var parts uint32 = 30
  step := diff/parts 
  
  var ippps string = "0"
  for i := uint32(0); i < parts; i++ {
    //вместо принта шлю зону ботам
    if ippps != "0"{
      fmt.Println(ippps+"-"+inet_ntoa(ip0 + step*i))
    }
          ippps = inet_ntoa(ip0 + step*i)
  }
  
}

V
Vladimir Kuts, 2016-04-23
@fox_12

I don’t know the details of your task, but I would solve the problem somehow like this:
on one master machine, I would launch a generator like (code in python):

>>> def get_ip():
...    for a in range(226):
...        for b in range(1,225):
...            ip = '127.0.{a}.{b}'.format(a=a, b=b)
...            yield ip
... 
>>> get_ip()
<generator object get_ip at 0x101d5ea50>
>>> my_ips = get_ip()

А машины-воркеры бы брали очередной адрес после отработки предыдущего как-то так:
>>> next(my_ips)
'127.0.0.1'
>>> next(my_ips)
'127.0.0.2'
>>> next(my_ips)
'127.0.0.3'
>>> next(my_ips)
'127.0.0.4'

And run scans on at least a million machines. Such a solution is freely scalable to any number of machines, and evenly loads all workers equally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question