Skip to content

Instantly share code, notes, and snippets.

@finnigja
Created May 26, 2023 21:29
Show Gist options
  • Save finnigja/e195d54275ba36da1670cff42dfb4d00 to your computer and use it in GitHub Desktop.
Save finnigja/e195d54275ba36da1670cff42dfb4d00 to your computer and use it in GitHub Desktop.
Simple nmap scanner / differ
#!/usr/local/bin/bash
SCAN_TARGETS="x.x.x.x/28 x.x.x.x/27 x.x.x.x"
EMAIL_RECIPIENTS="email@example.com.com"
WEB_URL="https://tools.example.com/scans/scan-$DATE.xml"
RUN_DIRECTORY="/usr/local/scans/data/"
NMAP="/usr/local/bin/nmap"
NDIFF="/usr/local/bin/ndiff"
DATE=`date +%FT%T%z`
echo "`date` - AutoNMAP beginning run. "
cd $RUN_DIRECTORY || exit 2
echo "`date` - Executing nmap, please wait. This may take a while."
$NMAP --open -T4 -PN -sV $SCAN_TARGETS -n -oX scan-$DATE.xml --stylesheet "nmap.xsl" > /dev/null
echo "`date` - Process completed with exit code $?"
if [ -e scan-latest.xml ]
then
echo "`date` - Running ndiff..."
DIFF=`$NDIFF scan-latest.xml scan-$DATE.xml 2>&1`
echo "`date` - Checking ndiff output"
# there are always two lines of difference, the run header that has the time/date in; ignore that
if [ `echo "$DIFF" | wc -l` -gt 2 ]
then
echo "`date` - Differences detected."
echo -e "AutoNMAP ***detected changes*** in network footprint. \n\nTargets scanned: ${SCAN_TARGETS}\n\nDiff output:\n$DIFF\n\nFull report available at $WEB_URL" | mail -s "$(echo -e "AutoNMAP network scan results\nImportance: High")" $EMAIL_RECIPIENTS
else
echo "`date`- No differences detected."
echo -e "AutoNMAP did not detect changes in network footprint. \n\nTargets scanned: ${SCAN_TARGETS}\n\nDiff output:\n$DIFF\n\nFull report available at $WEB_URL" | mail -s "$(echo -e "AutoNMAP network scan results\nImportance: Low")" $EMAIL_RECIPIENTS
fi
else
echo "`date` - There is no latest scan (scan-latest.xml). Cannot diff this run; will do next run."
fi
ln -sf scan-$DATE.xml scan-latest.xml
echo "`date` - AutoNMAP is complete."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment