#!/bin/bash

# Give paths to nmap executables as arguments. Log files go in subdirectories
# named after the benchmark $ID and $HOSTNAME. Subdirectories are identified by
# the name of the directory containing each executable. For example, logs for
# ../nmap-perf/nmap will go in nmap-perf-<round>/. If an argument is just
# "nmap" with no directory, its logs go in nmap-<round>/.

if [ $# -lt 1 ]; then
	echo "Usage: $0 <nmap> [<nmap>]..."
	exit 1
fi

NMAPS=$*

COMMON_OPTIONS="-sP -n -d2 --max-retries 1 -iL ping-hosts"

# ID=`date +%Y-%m-%d`
HOSTNAME=${HOSTNAME:-`hostname`}

FIRST_ROUND=1
LAST_ROUND=4

function get_nick() {
	nick=$(basename $(dirname $1))
	if [ "$nick" = "." ]; then
		nick=nmap
	fi
	echo $nick
}

function run_nmap() {
	scan=$1
	shift
	for nmap in $NMAPS; do
		dir=${ID:+$ID/}$HOSTNAME/`get_nick $nmap`-$round
		mkdir -p $dir
		echo "$nmap $* --datadir $(dirname $nmap) -oX $dir/$scan.xml > $dir/$scan.nmap 2>&1"
		$nmap $* --datadir $(dirname $nmap) -oX $dir/$scan.xml > $dir/$scan.nmap 2>&1
	done
}

for round in `seq $FIRST_ROUND $LAST_ROUND`; do
	run_nmap ping-PS $COMMON_OPTIONS
	run_nmap ping-PE $COMMON_OPTIONS
done

tar cjvf nmap-bench-$HOSTNAME${ID:+-$ID}.tar.bz2 ${ID:+$ID/}$HOSTNAME/

echo nmap-bench-$HOSTNAME${ID:+-$ID}.tar.bz2
