Update Parser.py

Added function docstrings, comments, PEP8 compliance
This commit is contained in:
S0Ulle33 2019-01-24 13:09:00 +03:00
parent 825b89e62a
commit 7c8356ca4d

View File

@ -2,34 +2,51 @@ import ipaddress
def getPortsFromString(ports): def getPortsFromString(ports):
# Converts ports from form 20-40,100-900,40000-70000 """
# It will automatically prune off non-existent ports (<1 >65535) Parses ports from string, returns them as integers in the list.
if ports is None: Handles non-existent ports and non-port values.
return [21, 22, 23, 25, 80, 443, 110, 111, 135, 139, 445, 8080, 8443, 53, 143, 989, 990, 3306, 1080, 5554, 6667, 2222, 4444, 666, 6666, 1337, 2020, 31337] # Change to default ports from constant """
else: if ports:
if "-" not in ports: # Using set to avoid repetitions
tports = ports.split(",") parsed = set()
print(tports)
else:
ports = ports.split(",") ports = ports.split(",")
tports = []
for port in ports: for port in ports:
if "-" not in port: try:
tports.append(int(port)) # Input is in range form ("100-200"):
if '-' in port:
start, end = map(int, port.split('-'))
parsed.update(
[p for p in range(start, end + 1) if 65355 >= p > 0])
# Input is a single port ("80"):
else: else:
# I made this one line because I wanted to parsed.add(int(port))
tports.extend( except ValueError as e:
list(range(int(port.split("-")[0]), int(port.split("-")[1]) + 1))) # If we get any not integer just ignore it
ports = [int(n) for n in tports if int(n) > 0 and int(n) < 65536] pass
return ports return sorted(list(parsed))
else:
# Change to default ports from constant
return [21, 22, 23, 25, 80, 443, 110, 111, 135, 139, 445, 8080, 8443, 53, 143, 989, 990, 3306, 1080, 5554, 6667, 2222, 4444, 666, 6666, 1337, 2020, 31337]
def getCIDRFromRanges(ips): def getCIDRFromRanges(ips):
"""
Parses ip input string, returns the generator over them.
Supports next inputs:
1) 1.2.3.4
2) 192.168.0.0/24
3) 1.2.3.4 - 5.6.7.8
Any non-ip value will be ignored.
"""
# A set to contain non repeating ip objects from ipaddress
ip_objects = set() ip_objects = set()
inputs = [ip.strip() for ip in ips.split(',')] inputs = [ip.strip() for ip in ips.split(',')]
for input_ in inputs: for input_ in inputs:
try: try:
# Input is in range form ("1.2.3.4 - 5.6.7.8"):
if '-' in input_: if '-' in input_:
input_ips = input_.split('-') input_ips = input_.split('-')
ranges = {ipaddr for ipaddr in ipaddress.summarize_address_range( ranges = {ipaddr for ipaddr in ipaddress.summarize_address_range(
@ -37,18 +54,23 @@ def getCIDRFromRanges(ips):
ipaddress.IPv4Address(input_ips[1])) ipaddress.IPv4Address(input_ips[1]))
} }
ip_objects.update(ranges) ip_objects.update(ranges)
# Input is in CIDR form ("192.168.0.0/24"):
elif '/' in input_: elif '/' in input_:
network = ipaddress.ip_network(input_) network = ipaddress.ip_network(input_)
ip_objects.add(network) ip_objects.add(network)
# Input is a single ip ("1.1.1.1"):
else: else:
ip = ipaddress.ip_address(input_) ip = ipaddress.ip_address(input_)
ip_objects.add(ip) ip_objects.add(ip)
except ValueError as e: except ValueError as e:
print(e) # If we get any non-ip value just ignore it
pass
for ip_obj in ip_objects: for ip_obj in ip_objects:
if not isinstance(ip_obj, ipaddress.IPv4Address): # The object is just one ip, simply yield it:
for ip in ip_obj.hosts(): if isinstance(ip_obj, ipaddress.IPv4Address):
yield ip
else:
yield ip_obj yield ip_obj
# The object is a network, yield every host in it:
else:
for host in ip_obj.hosts():
yield host