Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Other Things Gentoo
  • Search

Decompose a range of IP addresses

Still need help with Gentoo, and your question doesn't fit in the above forums? Here is your last bastion of hope.
Post Reply
Advanced search
15 posts • Page 1 of 1
Author
Message
ikaro
Advocate
Advocate
User avatar
Posts: 2527
Joined: Mon Jul 14, 2003 2:04 pm
Location: Denmark

Decompose a range of IP addresses

  • Quote

Post by ikaro » Wed May 12, 2004 4:43 am

Hi,

I got a file with a load of ip ranges ( 123.123.123.13-145.155.155.155)

Now i want to decompose/convert that file with the huge list into a new file, where the addresses are in network/host format.

example:
file1: 199.232.0.0-199.232.255.255

new file: 199.232.0.0/16

off course, some solution must be able to read "file1" and convert all the ipranges into the new format and create file2.

ive been looking everywhere and couldnt find any tool do make this
:roll:


Thanks.
linux: #232767
Top
petrjanda
Veteran
Veteran
User avatar
Posts: 1557
Joined: Fri Sep 05, 2003 10:04 pm
Location: Brno, Czech Republic
Contact:
Contact petrjanda
Website

  • Quote

Post by petrjanda » Wed May 12, 2004 7:53 am

Hmm, the number behind the slash is not a host portition of an IP address, it's the number of bits borrowed (i think? my memory is not good lately). What exactly do you mean by network/host format?
There is, a not-born, a not-become, a not-made, a not-compounded. If that unborn, not-become, not-made, not-compounded were not, there would be no escape from this here that is born, become, made and compounded. - Gautama Siddharta
Top
rewt
n00b
n00b
User avatar
Posts: 58
Joined: Thu Feb 19, 2004 9:38 am

  • Quote

Post by rewt » Wed May 12, 2004 8:01 am

The number indicates where the network identifying part of the IP address stops and the host identifying part begins which is what he means. It's just a shorthand form of the netmask indicating how many 1's are in it in the binary form which I think is what you mean by bits borrowed?

As to a program to do this... no idea, sorry
Because sometimes peace is another word for surrender... and secrets have a way of getting out
Top
petrjanda
Veteran
Veteran
User avatar
Posts: 1557
Joined: Fri Sep 05, 2003 10:04 pm
Location: Brno, Czech Republic
Contact:
Contact petrjanda
Website

  • Quote

Post by petrjanda » Wed May 12, 2004 8:18 am

rewt wrote:The number indicates where the network identifying part of the IP address stops and the host identifying part begins which is what he means. It's just a shorthand form of the netmask indicating how many 1's are in it in the binary form which I think is what you mean by bits borrowed?

As to a program to do this... no idea, sorry
sounds like it.
For example, i have /18, therefore its a class B network with 2 bits borrowed from the host portition? right? Therefore the subnet mask would be 255.255.192.0?
There is, a not-born, a not-become, a not-made, a not-compounded. If that unborn, not-become, not-made, not-compounded were not, there would be no escape from this here that is born, become, made and compounded. - Gautama Siddharta
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Wed May 12, 2004 10:06 am

Looks like a job for python - although you could cheat and do it in sed.
How regular are the address ranges? Could you post a few lines of the file?
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
DaveArb
Guru
Guru
Posts: 510
Joined: Thu Apr 29, 2004 2:46 pm
Location: Texas, USA

Re: Decompose a range of IP addresses

  • Quote

Post by DaveArb » Wed May 12, 2004 2:11 pm

ikaro wrote:where the addresses are in network/host format.
Those are more commonly called CIDR addresses or ranges, that might help you in finding resources.

Sendmail comes with a user-contributed PERL program called `cidrexpand` that takes CIDR addresses and expands them to Sendmail access-acceptable entries. That program might help you with a headstart in writing one, if you do PERL.

From experience, the real PITA is if the input IP ranges are not normalized, ie. if they don't map one for one into a single CIDR.

Dave
Top
ikaro
Advocate
Advocate
User avatar
Posts: 2527
Joined: Mon Jul 14, 2003 2:04 pm
Location: Denmark

  • Quote

Post by ikaro » Wed May 12, 2004 2:17 pm

ecatmur wrote:Looks like a job for python - although you could cheat and do it in sed.
How regular are the address ranges? Could you post a few lines of the file?
sure

Code: Select all

199.232.0.0-199.232.255.255
194.76.38.0-194.76.39.255
195.37.190.41-195.37.190.41
80.146.186.80-80.146.186.87
195.243.81.0-195.243.81.7
217.19.187.0-217.19.187.15
217.19.187.64-217.19.187.95
217.19.187.96-217.19.187.103
81.209.246.15-81.209.246.15
62.67.62.15-62.67.62.15
193.7.0.0-193.7.127.255
62.154.151.32-62.154.151.47
212.185.124.72-212.185.124.79
62.156.153.103-62.156.153.104
62.134.136.10-62.134.136.10
62.154.204.168-62.154.204.175
195.145.34.96-195.145.34.111
212.111.240.0-212.111.240.127
...
linux: #232767
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Wed May 12, 2004 3:39 pm

OK, this is possibly the most obvious solution:

Code: Select all

#!/usr/bin/python
import sys

def log2(x, n=0):
	if x==0: return n
	return log2(x>>1, n+1)

def range_to_cidr(range):
	try:
		lower, upper = map(lambda x: x.split('.'), range.split('-'))
		bits = 32 - sum(map(lambda l, u: log2(int(l)^int(u)), lower, upper))
		return "%s/%d" % (".".join(lower), bits)
	except ValueError, TypeError:
		return ""

print "\n".join([range_to_cidr(x.split()[0]) for x in sys.stdin.readlines()])
Last edited by ecatmur on Wed May 12, 2004 9:27 pm, edited 1 time in total.
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Wed May 12, 2004 4:59 pm

Another, more functional solution, showing off some advanced python features:

Code: Select all

#!/usr/bin/python
from sys import stdin
from math import frexp
from operator import xor, getitem
from string import split

def compose(*funs):
	if len(funs)==0: return lambda x: x
	elif len(funs)==1: return funs[0]
	else: return lambda *args: funs[0](compose(*funs[1:])(*args))
partial  = lambda fun, *l_args: lambda *r_args: fun(*(l_args+r_args))
rpartial = lambda fun, *r_args: lambda *l_args: fun(*(l_args+r_args))

log2 = compose(rpartial(getitem, 1), frexp)
ip4_to_quad = compose(partial(map, int), rpartial(split, '.'))

def range_to_cidr(lower_ip, upper_ip):
	try:
		lower, upper = map(ip4_to_quad, (lower_ip, upper_ip))
		bits = 32 - sum(map(compose(log2, xor), lower, upper))
		return "%s/%d" % (lower_ip, bits)
	except ValueError, TypeError:
		return ""

def range_list_to_cidr_list(list):
	for range in list:
		yield range_to_cidr(*range.split()[0].split('-'))

for x in range_list_to_cidr_list(stdin.readlines()):
	print x
Last edited by ecatmur on Wed May 12, 2004 9:28 pm, edited 1 time in total.
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
ikaro
Advocate
Advocate
User avatar
Posts: 2527
Joined: Mon Jul 14, 2003 2:04 pm
Location: Denmark

  • Quote

Post by ikaro » Wed May 12, 2004 5:10 pm

erm .. I appreciate the effort, but how does it works ?
im trying to run ./file.py file-with-the-ranges and nothing happens ....
linux: #232767
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Wed May 12, 2004 5:50 pm

It operates on stdin, so run it with:
cat file-with-the-ranges | ./file.py
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
ikaro
Advocate
Advocate
User avatar
Posts: 2527
Joined: Mon Jul 14, 2003 2:04 pm
Location: Denmark

  • Quote

Post by ikaro » Wed May 12, 2004 7:23 pm

HI.

I think its almost there, but not quite.

for example 194.76.38.0-194.76.39.255 is 194.76.38.0/23
but your script says 194.76.38.0/9

Thanks until now :)
linux: #232767
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Wed May 12, 2004 9:29 pm

Oh yeah, I forgot to subtract from 32. It's fixed now above.
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
ikaro
Advocate
Advocate
User avatar
Posts: 2527
Joined: Mon Jul 14, 2003 2:04 pm
Location: Denmark

  • Quote

Post by ikaro » Thu May 13, 2004 3:53 pm

Thank you _very much_ :)
I hope it was good to solve this problem.
linux: #232767
Top
ecatmur
Advocate
Advocate
User avatar
Posts: 3595
Joined: Mon Oct 20, 2003 8:07 pm
Location: Edinburgh
Contact:
Contact ecatmur
Website

  • Quote

Post by ecatmur » Thu May 13, 2004 6:22 pm

It was actually, I got to learn some new Python features that are on the way soon.
Try and read the first program, see if you can work out how it works. Python tends to be very easy to read.
No more cruft
dep: Revdeps that work
Using command-line ACCEPT_KEYWORDS?
Top
Post Reply

15 posts • Page 1 of 1

Return to “Other Things Gentoo”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic