Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
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")
>>> is_valid("531")
>>> is_valid("3531333")
>>> is_valid("664")
>>> is_valid("10")
periode = len(siteswap)
chiffres = [(int(chiffre) + tps) % periode \
for tps, chiffre in enumerate(siteswap)]
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 ipv4 range :
>>> ttnn_ips = jugglable_ip(iptools.IpRange(""))
('', '912241494')
Traceback (most recent call last):
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
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:
elif args.nb_obj_max > 0 and nb_obj > args.nb_obj_max:
# pretty print of the IP
if args.check_on_last == 4:
ip_str = ip_addr
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__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.