scripts/bash/common.sh
#!/usr/bin/env bash

COLOR_CYAN_BOLD="\e[1;36m"
COLOR_GREEN_BOLD="\e[1;32m"
COLOR_YELLOW_BOLD="\e[1;33m"
COLOR_RED_BOLD="\e[1;31m"
COLOR_RST="\e[0m"

SIGN_INFO="[*]"
SIGN_SUCCESS="[+]"
SIGN_WARNING="[!]"
SIGN_ERROR="[-]"

TOOL_IMPACKET_SECRETSDUMP="impacket-secretsdump"

FOLDER_NAME_NMAP="nmap"
FOLDER_NAME_RECON="recon"
FOLDER_NAME_LOOT="loot"

function print_info() {
    echo -e "${COLOR_CYAN_BOLD}${SIGN_INFO}${COLOR_RST} $1"
}

function print_success() {
    echo -e "${COLOR_GREEN_BOLD}${SIGN_SUCCESS}${COLOR_RST} $1"
}

function print_warning() {
    echo -e "${COLOR_YELLOW_BOLD}${SIGN_WARNING}${COLOR_RST} $1" >&2
}

function print_error() {
    echo -e "${COLOR_RED_BOLD}${SIGN_ERROR}${COLOR_RST} $1" >&2
}

function check_argc() {
    if [ $1 -lt $2 ];
    then
        print_error "Invalid number of arguments: ${1} (${2} expected)"
        return 1
    else
        return 0
    fi
}

function get_timestamp() {
    date "+%F_%H-%M-%S"
}

function test_is_root() {
    if [ $(id -u) -ne 0 ];
    then
        print_error "Current user is not root."
        return 1
    fi
    return 0
}

function test_is_not_root() {
    if [ $(id -u) -eq 0 ];
    then
        print_error "Current user is root."
        return 1
    fi
    return 0
}

function test_directory_exists() {
    if [ -d "${1}" ]
    then
        return 0
    else
        print_error "Directory does not exist: ${1}"
        return 1
    fi
}

function test_file_exists() {
    if [ -f "${1}" ]
    then
        return 0
    else
        print_error "File does not exist: ${1}"
        return 1
    fi
}

function test_network_interface_exists() {
    ip link show $1 >/dev/null 2>&1
    rc=$?
    if [ $rc -ne 0 ];
    then
        print_error "Network interface not found: ${1}"
    fi
    return $rc
}

function test_command_exists() {
    which $1 >/dev/null 2>&1
    rc=$?
    if [ $rc -ne 0 ];
    then
        print_warning "Command not found: ${1}"
    fi
    return $rc
}