Skip to content

Instantly share code, notes, and snippets.

@enavarro222
Last active December 17, 2015 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enavarro222/5590544 to your computer and use it in GitHub Desktop.
Save enavarro222/5590544 to your computer and use it in GitHub Desktop.
Stupide script that list all jugglable IP adress of a range (ipv4 only)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
""" Petit script bourrin qui liste toutes les IP jonglables d'une plage donnée
Dépend de iptools: http://code.google.com/p/python-iptools/
Auteur: Emmanuel Navarro
Date: 20130516
"""
import sys
import argparse
import iptools
def is_valid(siteswap):
""" Determine if a siteswap is valid
siteswap: str
>>> is_valid("3")
True
>>> is_valid("531")
True
>>> is_valid("3531333")
True
>>> is_valid("664")
False
>>> is_valid("10")
False
"""
periode = len(siteswap)
chiffres = [(int(chiffre) + tps) % periode \
for tps, chiffre in enumerate(siteswap)]
chiffres.sort()
return chiffres == range(len(chiffres))
def nb_objects(siteswap):
""" Determine the number of object of a siteswap
siteswap: str
"""
return sum(int(chiffre) for chiffre in siteswap) / len(siteswap)
def jugglable_ip(ip_range, check_on_last=4):
""" Return a iterable over all jugglable IP of a given IpRange
ip_range: iptools.IpRange object
check_on_last: only consider the last 'check_on_last' figures of the IP
yields pairs (ip_adds, siteswap) both are str
An example over tetaneutral.net ipv4 range :
>>> ttnn_ips = jugglable_ip(iptools.IpRange("91.224.148.0/23"))
>>> ttnn_ips.next()
('91.224.149.4', '912241494')
>>> ttnn_ips.next()
Traceback (most recent call last):
...
StopIteration
"""
for ip_addr in ip_range:
siteswap = "".join(ip_addr.split(".")[-check_on_last:])
if is_valid(siteswap):
yield (ip_addr, siteswap)
def main():
""" Small brute force script that list all jugglable IP addresses
"""
# first run the tests
import doctest
doctest.testmod()
parser = argparse.ArgumentParser(description=main.__doc__)
parser.add_argument('iprange', metavar='IPs', type=str,
help='Ip range to check')
parser.add_argument('-l', dest='check_on_last', type=int, default=4,
help='Number of "last" figure to consider')
parser.add_argument('-m', dest='nb_obj_min', type=int, default=0,
help='Minimum number of objects')
parser.add_argument('-M', dest='nb_obj_max', type=int, default=-1,
help='Maximum number of objects')
args = parser.parse_args()
ip_range = iptools.IpRange(args.iprange)
ip_ok = jugglable_ip(ip_range, check_on_last=args.check_on_last)
# list all valid IP, filter by number of objects
for ip_addr, siteswap in ip_ok:
nb_obj = nb_objects(siteswap)
if nb_obj < args.nb_obj_min:
continue
elif args.nb_obj_max > 0 and nb_obj > args.nb_obj_max:
continue
# pretty print of the IP
if args.check_on_last == 4:
ip_str = ip_addr
else:
splited = ip_addr.split(".")
ip_begin = ".".join(splited[:-args.check_on_last])
ip_end = ".".join(splited[-args.check_on_last:])
ip_str = "[%s.]%s" % (ip_begin, ip_end)
print("%d\t%12s\t%s" % (nb_obj, siteswap, ip_str))
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment