Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compare feature for checking matches for IPs in file A in file B #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Split given IP ranges in equally sized slices (e.g. 10 times 500 IP addresses)

```EXAMPLE: --fileIpAddresses <text_file_with_ip_addresses> --splitIpRangeInSlices 10```

Compare IP ranges by identifying matches of IP list A (fileIpAddresses) in IP list B (fileIpSubtracts)

```EXAMPLE: --fileIpAddresses <text_file_with_ip_addresses> --fileIpSubtracts --compareIpRanges True```

# Parameters
```
-h, --help show this help message and exit
Expand All @@ -39,4 +43,6 @@ Split given IP ranges in equally sized slices (e.g. 10 times 500 IP addresses)
(optional) size of the network in CIDR (e.g. 24 for /24) for plausibility checks
-t SPLITIPRANGEINSLICES, --splitIpRangeInSlices SPLITIPRANGEINSLICES
(optional) split IP networks in equally sized slices (e.g. 10 times 500 IP addresses
-c COMPAREIPRANGES, --compareIpRanges COMPAREIPRANGES
(optional) mode to compare the IP files by identifying matches from flag -f in flag -s
```
53 changes: 49 additions & 4 deletions net-consolidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

* Split given IP ranges in equally sized slices (e.g. 10 times 500 IP addresses)
EXAMPLE: --fileIpAddresses <text_file_with_ip_addresses> --splitIpRangeInSlices 10

* Compare IP ranges by identifying matches of IP list A (fileIpAddresses) in IP list B (fileIpSubtracts)
EXAMPLE: --fileIpAddresses <text_file_with_ip_addresses> --fileIpSubtracts --compareIpRanges True

PARAMETER
---------
Expand All @@ -55,6 +58,7 @@
parser.add_argument('-p', '--plausibilityChecks', help='(optional) enable plausibility checks (e.g. subnet size)')
parser.add_argument('-n', '--subnetSize', help='(optional) size of the network in CIDR (e.g. 24 for /24) for plausibility checks')
parser.add_argument('-t', '--splitIpRangeInSlices', help='(optional) split IP networks in equally sized slices (e.g. 10 times 500 IP addresses')
parser.add_argument('-c', '--compareIpRanges', help='(optional) mode to compare the IP files by identifying matches from flag -f in flag -s')
args = parser.parse_args()

# check: parameter fileIpAddresses is a file
Expand Down Expand Up @@ -182,7 +186,7 @@ def checkIpOnPlausibility( ip ):
# Subtract IP ranges for delta between IP network A and B
#########################################################
def subtractIpRanges( fileIpAddr, fileIpSub ):
"""
"""
@type fileIpAddr: String
@param fileIpAddr: Filename of a file with IP addresses
@type fileIpSub: String
Expand All @@ -192,13 +196,34 @@ def subtractIpRanges( fileIpAddr, fileIpSub ):
"""
# parse the files for IP addresses
ip_list_unscanned = readIpFile ( fileIpAddr )
ip_list_scanned = readIpFile ( fileIpSub )
ip_list_scanned = readIpFile ( fileIpSub )
# Subtract the ip lists
ip_list_toscan = ip_list_unscanned - ip_list_scanned
# Merge the result ip list
ip_list_toscan_merge = cidr_merge(ip_list_toscan)
return ip_list_toscan_merge

# Compare IP ranges
#########################################################
def compareIpRanges( fileIpA, fileIpB ):
"""
@type fileIpA: String
@param fileIpA: Filename of a file with IP addresses
@type fileIpB: String
@param fileIpB: Filename of a file with IP addresses
@rtype: IPSet()
@return: List of IP ranges
"""
# parse the files for IP addresses
ip_list_A = readIpFile ( fileIpA )
ip_list_B = readIpFile ( fileIpB )
# Identify matches of list A in list B
compare_result = []
for ip in ip_list_A:
if ip in ip_list_B:
compare_result.append(ip)
return compare_result

# Split IP ranges into slices each of the same size
#####################################################
def splitIpRanges( numberOfSlices, ip_list ):
Expand Down Expand Up @@ -257,14 +282,35 @@ def splitIpRanges( numberOfSlices, ip_list ):
# Main
######

# Case: Compare IP ranges in file2 from file1
if args.compareIpRanges:
ip_list = compareIpRanges(fileIpAddr, fileIpSub)

# Check plausibility
if args.plausibilityChecks:
for ip in ip_list:
if not ( checkIpOnPlausibility( '%s' % ip )):
print ("[*] IP list did not pass the plausibility checks ...")
exit()

if args.splitIpRangeInSlices:
splitIpRanges( args.splitIpRangeInSlices, ip_list )

if not args.splitIpRangeInSlices:
# Print result list
for ip in ip_list:
print('%s' % ip)
exit()


# Case: Substracting IP ranges in file2 from file1
if args.fileIpSubtracts:
ip_list = subtractIpRanges(fileIpAddr, fileIpSub)

# Check plausibility
if args.plausibilityChecks:
for ip in ip_list:
if not ( checkIpOnPlausibility( '%s' % ip )):
if not ( checkIpOnPlausibility( '%s' % ip )):
print ("[*] IP list did not pass the plausibility checks ...")
exit()

Expand All @@ -277,7 +323,6 @@ def splitIpRanges( numberOfSlices, ip_list ):
print('%s' % ip)
exit()


# Case: Only Merging IP ranges
elif args.fileIpAddresses:
ip_list = mergeIpRanges(fileIpAddr)
Expand Down