Created
June 17, 2011 07:15
-
-
Save mapcuk/1031004 to your computer and use it in GitHub Desktop.
Script gets range between 2 ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Test task""" | |
def ntoa(num): | |
c = 2**8 | |
ip=[] | |
k=range(4) | |
k.reverse() | |
for i in k: | |
p, num = divmod(num,c**i) | |
ip.append(str(p)) | |
return '.'.join(ip) | |
def aton(IP): | |
numh = ''.join(["%02x" % long(oc) for oc in IP.split('.')]) | |
num = long(numh,16) | |
return num | |
def print_range(ip1,ip2): | |
adr1 = aton(ip1) | |
adr2 = aton(ip2) | |
for i in xrange(adr1, adr2+1): | |
print ntoa(i) | |
def test1(): | |
ips = ('127.0.0.1','127.0.0.6', '232.78.90.120') | |
for ip in ips: | |
print 'IP: %s' %ip | |
n = aton(ip) | |
print 'num %i' % n | |
print ntoa(n) | |
def test2(): | |
ip1='127.0.0.1' | |
ip2='127.0.0.6' | |
print_range(ip1, ip2) | |
def main(): | |
ip1 = raw_input('Please enter first IP: ') | |
ip2 = raw_input('... and second IP: ') | |
if not (ip1 and ip2): #simple check could be harder with regex | |
print 'Bad IPs' | |
return False | |
print_range(ip1,ip2) | |
return True | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment