best way to get all possible ips from network writing in cidr

7,390 views
Skip to first unread message

Vasiliy Tolstov

unread,
Mar 15, 2013, 5:52:24 AM3/15/13
to golan...@googlegroups.com
Hello. I need to scan some ips that runs my own server and check,
does it responds.
For example i have network 62.76.47.12/16
After net.ParseCIDR i have IPNet that contains ip and mask, what is
the best way to get all ips in that range and iterate over it?

--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru
jabber: va...@selfip.ru

Russ Cox

unread,
Mar 15, 2013, 12:27:34 PM3/15/13
to Vasiliy Tolstov, golang-nuts

Vasiliy Tolstov

unread,
Mar 16, 2013, 11:27:06 AM3/16/13
to Russ Cox, golang-nuts
Wow, very good. Thanks, Russ!
If ipnet.Contains(ip) runs fast? I'm try to do something converting ip
to binary and increment it...

2013/3/15 Russ Cox <r...@golang.org>:
> http://play.golang.org/p/m8TNTtygK0

Russ Cox

unread,
Mar 18, 2013, 10:17:33 AM3/18/13
to Vasiliy Tolstov, golang-nuts
On Sat, Mar 16, 2013 at 11:27 AM, Vasiliy Tolstov <v.to...@selfip.ru> wrote:
Wow, very good. Thanks, Russ!
If ipnet.Contains(ip) runs fast? I'm try to do something converting ip
to binary and increment it...

It should be fast enough.
You can measure it.
Saving the output will be much more expensive than the calls.

Russ

Vasiliy Tolstov

unread,
Mar 19, 2013, 1:27:39 AM3/19/13
to Russ Cox, golang-nuts
2013/3/18 Russ Cox <r...@golang.org>:
> It should be fast enough.
> You can measure it.
> Saving the output will be much more expensive than the calls.


Thanks. I'm not store ips a pass it via channel to scanner and returns
result via channel back.

ziutek

unread,
Mar 20, 2013, 5:04:41 AM3/20/13
to golang-nuts
One thing I'm missing in net package is function like:

func (ip IP) Add(offset int) IP

I've written such utility function for my needs, see:
http://godoc.org/github.com/ziutek/utils/netaddr#IPAdd

Vasiliy Tolstov

unread,
Mar 20, 2013, 9:22:55 AM3/20/13
to ziutek, golang-nuts
2013/3/20 ziutek <ziu...@lnet.pl>:
Hm, what is about ipv6 and Add function? As i see some functions
expect to see only ipv4

ziutek

unread,
Mar 20, 2013, 9:45:56 AM3/20/13
to golang-nuts


On 20 Mar, 14:22, Vasiliy Tolstov <v.tols...@selfip.ru> wrote:
> Hm, what is about ipv6 and Add function? As i see some functions
> expect to see only ipv4

One thing that I love about net package is that it treats IPv4 and
IPv6 in unified way. So Add function shouldn't be an exception.
netaddr.IPAdd function works in this way, see tests:

https://github.com/ziutek/utils/blob/master/netaddr/ip_test.go

Vasiliy Tolstov

unread,
Mar 20, 2013, 1:27:31 PM3/20/13
to ziutek, golang-nuts
2013/3/20 ziutek <ziu...@lnet.pl>:
> One thing that I love about net package is that it treats IPv4 and
> IPv6 in unified way. So Add function shouldn't be an exception.
> netaddr.IPAdd function works in this way, see tests:
>
> https://github.com/ziutek/utils/blob/master/netaddr/ip_test.go


Ok. Very good =)
Reply all
Reply to author
Forward
0 new messages