11-20-2016, 12:24 PM
ips save like this :- 182.192.183.12
Code:
import sys
import re
import os
# Check for enough parameters
if len(sys.argv) < 3:
print "Usage: rangemaker.py infile outfile"
print "Example: rangemaker.py ips.txt out.txt"
sys.exit
ipsfile = sys.argv[1]
outfile = sys.argv[2]
# Check if the ips file exists.
if not os.path.isfile(ipsfile):
print "{0} not found!".format(ipsfile)
sys.exit()
def make_range(ip):
"""Make range from ip"""
# Check if the ip is valid
ip_pattern = r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
match = re.match(ip_pattern, ip)
if match:
ip_list = ip.split(".")[:2]
end = ".".join(ip_list + ["255"]*2)
return "{start}-{end}\n".format(start=ip,
end=end)
hOut = open(outfile, "a")
with open(ipsfile, "r") as hIps:
for line in hIps:
# Strip newline and skip empty lines
line = line.strip()
if line == "":
continue
range_string = make_range(line)
if range_string:
hOut.write(range_string)
hOut.close()












