03_ad/ad-nmap-host.sh
#!/usr/bin/env bash

### BEGIN SCRIPT HEADER
SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
SCRIPT_NAME=$(basename "${SCRIPT_PATH}")
DIR_SCRIPT=$(dirname "${SCRIPT_PATH}")
DIR_ROOT=$(dirname $DIR_SCRIPT)
HELPER="${DIR_ROOT}/01_pwnbox/tools-helper.sh"
source "$HELPER" || exit
### END SCRIPT HEADER

### BEGIN COMMON
function print_usage_and_exit() {
    print_info "Usage: ${SCRIPT_NAME} <HOSTNAME>"
    exit
}

check_argc $# 1 || print_usage_and_exit
### END COMMON

test_directory_exists "./${FOLDER_NAME_NMAP}" || exit

TIMESTAMP=$(get_timestamp)
HOSTNAME=$(echo -n $1 | tr '[:upper:]' '[:lower:]')
OUT_FILE_NMAP_PREFIX="./${FOLDER_NAME_NMAP}/nmap_host_${HOSTNAME}_${TIMESTAMP}"

host "${HOSTNAME}" >/dev/null 2>&1
if [ $? -ne 0 ]
then
    print_error "Failed to resolve hostname: ${HOSTNAME}"
    exit
fi

COMMAND="sudo ${TOOL_NMAP} -vvv -A -O --reason --open -p- --script *-vuln-* -oA \"${OUT_FILE_NMAP_PREFIX}\" \"${HOSTNAME}\""
eval ${COMMAND}
if [ $? -eq 0 ]
then
    print_success "Scan completed, result written to: ${OUT_FILE_NMAP_PREFIX}.nmap"
else
    print_warning "Scan failed."
fi
