public
Description: various scripts wrote by me
Homepage:
Clone URL: git://github.com/fishy/scripts.git
scripts / convertip.py
100755 46 lines (39 sloc) 0.971 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
 
import sys
import re
 
def ip2int(ip) :
ret = 0
match = re.match("(\d*)\.(\d*)\.(\d*)\.(\d*)", ip)
if not match : return 0
for i in xrange(4) : ret = (ret << 8) + int(match.groups()[i])
return ret
 
def int2ip(ipnum) :
ip1 = ipnum >> 24
ip2 = ipnum >> 16 & 0xFF
ip3 = ipnum >> 8 & 0xFF
ip4 = ipnum & 0xFF
return "%d.%d.%d.%d" % (ip1, ip2, ip3, ip4)
 
def printrange(startip, endip) :
bits = 1
mask = 1
while bits < 32 :
newip = startip | mask
if (newip>endip) or (((startip>>bits) << bits) != startip) :
bits = bits - 1
mask = mask >> 1
break
bits = bits + 1
mask = (mask<<1) + 1
newip = startip | mask
bits = 32 - bits
print "%s/%d" % (int2ip(startip), bits)
if newip < endip :
printrange(newip + 1, endip)
 
while 1 :
line = sys.stdin.readline().strip()
if not line : break
chars = line.split(" ")
print "#%s - %s" % (chars[0], chars[1])
ip1 = ip2int(chars[0])
ip2 = ip2int(chars[1])
printrange(ip1, ip2)