Skip to content

Instantly share code, notes, and snippets.

@adi928
Last active December 3, 2019 21:07
Show Gist options
  • Save adi928/3f7c98ef5e372c293fa8fd73532dc57b to your computer and use it in GitHub Desktop.
Save adi928/3f7c98ef5e372c293fa8fd73532dc57b to your computer and use it in GitHub Desktop.
Includes a function for scanning the network and running comparison on Nmap XML outputs. Needs Sanitizing
import nmap
from datetime import datetime
import difflib
from pathlib import Path
## Run nmap
def runNmap():
scanFileName = 'scan-' + str(datetime.now())
argStr = '-sC -sV'
nmapCmd = nmap.PortScanner()
nmapCmd.scan('xx.xx.com', '90-110', arguments=argStr)
with open(scanFileName, 'w') as fd:
fd.write(nmapCmd.get_nmap_last_output())
#print(nmapCmd.command_line())
## Get file
def getDifferences(output):
basePath = Path('.')
files = list(basePath.glob('scan-*'))
files = files[-2:]
## Get each line
f1 = open(str(files[0]), 'r')
f1_list = f1.read().split('\n')
f2 = open(str(files[1]), 'r')
f2_list = f2.read().split('\n')
# Using Differ class
if output == 1:
diff = difflib.Differ()
diff = diff.compare(f1_list, f2_list)
print('\n'.join(diff))
# Using HTML class
if output == 2:
diff = difflib.HtmlDiff()
with open('htmlTable.html', 'w') as web:
web.write(diff.make_file(f1_list, f2_list))
# for a in range(2):
# runNmap()
getDifferences(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment