#!/bin/bashNote that some lines are wrapped due to the shortage of space. This script assumes several things in order to run properly:
# Interface on which scan should be performed. Multiple interfaces
# should be separated with spaces!
SCAN_INTERFACES="eth1"
# Network that should be scanned. If empty, or undefined, automatically
# deduce network attached to interface. Note that if you specified
# multiple interfaces than this variable should be undefined!
SCAN_NETWORKS=
#######################################################################
# THERE ARE NO MORE CONFIGURABLE PARTS AFTER THIS LINE
#######################################################################
TIMESTAMP=`date +%Y%m%d%H%M`
START=`date +%Y%m%d%H%m%S.%N`
cd /var/log/nmap || exit 1
for if in \$SCAN_INTERFACES
do
# Find network to scan if it isn't specified...
[ -z "\$SCAN_NETWORKS" -o "\$if" != "\$SCAN_INTERFACES" ] && SCAN_NETWORKS=`/sbin/ip ro sh dev \$if | grep -v via | cut -f1 -d" "`
# Find addresses on the output interface so that we don't scan them
EXCLUDE_LIST=`/sbin/ip addr sh dev \$if | awk '/inet / {print "--exclude ", substr(\$2, 1, index(\$2, "/")-1)}'`
[ -z "\$SCAN_NETWORKS" ] && continue
# Start scanning
nmap -n -Pn -sS -O -sV -T4 -vv \${EXCLUDE_LIST} -oA nmap-\$if-\${TIMESTAMP} -e \$if ${SCAN_NETWORKS} >& nmap-scan-\$if-\${TIMESTAMP}.log
done
echo "START \$START END `date +%Y%m%d%H%m%S.%N`" >> /var/log/nmap-scan.log
exit 0
- You have a directory /var/log/nmap where all the result files will be placed.
- nmap is version 6, but definitely not 4 because version 4 has some weaknesses.
- You want to scan networks assigned to your interfaces.
- The script is run under root user.
Now, after each run of this script you'll have four files left in /var/log/nmap each with the following extension:
- nmap - this is a standard nmap output file
- gnmap - greppable nmap output
- xml - XML output file
- log - Log file into which stdout and stderr were redirected during nmap's run.
It is also necessary to configure script to be run periodically. cron is ideal for that purpose. To achieve that, you can add the following entry to root's crontab:
0 */2 * * * full_path_and_name_to_your_script
Obviously, you'll have to change full_path_and_name_to_your_script with exact path and filename. In this case, you'll get the script to be run every two hours.
No comments:
Post a Comment