#!/bin/bash
# This script was auto generated using the code-cleanup package http://forumubuntusoftware.info/viewtopic.php?f=23&t=9480
# Do a search and replace for ultimateedition.info followed by a search and replace REPLACEME with the name of your program & delete this line.

# ==============================================================================
# title			:REPLACEME
# description	:YOURDESCRIPTION
# author		:theemahn <theemahn@ultimateedition.info>
# date			:06/01/2025
# version		:1.0
# usage			:REPLACEME --help
# manual		:man REPLACEME
# notes			:See change-log below for further information.
# ==============================================================================
# Change-log: 1.0: unreleased
# ==============================================================================

# Modify the below information to correlate with the software you are designing.
APPNAME="sshdetect"
PROGNAME="sshdetect"
PROGRAMMER="TheeMahn"
BUILDDATE="07/12/2025"
VERSION="1.0.2"
DOMAIN="ultimateedition.info"
WEBSITE="${DOMAIN}"
EMAIL="<${PROGRAMMER}@${DOMAIN}>"


# Pull in common software
if [[ -f "ultimate-common" ]]; then
	source ultimate-common
elif [[ -f "/usr/share/ultimate_edition/ultimate-common" ]]; then
	source /usr/share/ultimate_edition/ultimate-common
else
	echo "No Ultimate Edition common source. Please install ultimate-edition-common."
	exit 0;
fi

# Turn off end user prompting
export DEBIAN_FRONTEND=noninteractive

# Help system - I have re-wrote this section to work with code-cleanup.
Help () {

	if [[ "${2}" == "" ]]; then
		VersionDump
		PRAM="ALL"
	else
		if ! [[ "${3}" ]]; then
			VersionDump
			PRAM="${2}"
		else
			PRAM="${2}"
		fi
	fi

	case "${PRAM}" in
		ALL)
		Encapsulate "Usage: ${PROGNAME} -<-COMMAND> [OPTION]"
		FullBar
		Encapsulate "Mandatory arguments to long options are identical for short options."
		Encapsulate "  possible commands..."
		Encapsulate " "
		Encapsulate "  -h		 --help		  this help message."
		Encapsulate "  -s		 --scan		  Scan the network fo ssh host(s)."
		Encapsulate "  -v		 --version	  dump version info and exit."
		Encapsulate " "
		FullBar
		Center "${PROGNAME} --help [COMMAND] for further information."
		FullBar;;

		# Version Help
		ALL|v|version)
		Encapsulate "Usage version;"
		FullBar
		Center "${PROGNAME} -v"
		FullBar
		Encapsulate "Displays ${PROGNAME}s version number and exits."
		FullBar;;

		# Help Help
		ALL|h|help|\?)
		Encapsulate "Usage Help [COMMAND]"
		FullBar
		Center "${PROGNAME} -h [COMMAND]"
		FullBar
		Encapsulate "Displays this message. For futher information ${PROGNAME} help [COMMAND]"
		Encapsulate "or refer to the manpages."
		Encapsulate " "
		Encapsulate "man ${PROGNAME}"
		Encapsulate " "
		Encapsulate "Example: ${PROGNAME} -h version"
		Encapsulate "Will display help about the command switch version"
		FullBar
	esac
	exit 0
}

Scan () {
	NetInfo
	INDEX=0
	Timer "Start" "${APPNAME}"
	declare -a SSHHOSTS=();
	declare -a HOSTNAMES=();
	IPS=$(ifconfig | grep -m1 -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | cut -d "." -f1,2,3)
	Encapsulate "Setting Internal IP Network Scan as ${IPS}.0/24"
	Encapsulate "Scanning entire network, please wait..."
	SSHHOSTS=$(sudo nmap -sS -p 22 192.168.1.0/24 | grep -B4 "open" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
	#echo "${SSHHOSTS}"
	SHIPS=($SSHHOSTS)
	NSSHIPS="${#SHIPS}"
	if [[ "${NSSHIPS}" -gt 0 ]]; then
		Encapsulate "${NSSHIPS} hosts detected."
		for EACH in "${SHIPS[@]}"
		do
			INDEX=$(( INDEX + 1 ))
			PCOMP=$((100*INDEX / NSSHIPS))
			ProgressBar "$PCOMP" "Processing IP: ${EACH} for hostname : ${INDEX} of ${NSSHIPS}"
			HOSTNAMES[${INDEX}]=$(nbtscan --system-dns "${EACH}" | grep "<server>" | cut -d " " -f5)
		done
		INDEX=0
		FullBar
		ControlColumnize -t "SSH HOST IP  " "HOSTNAME"
		FullBar
		for EACH in "${SHIPS[@]}"
		do
			INDEX=$(( INDEX + 1 ))
			ControlColumnize "${EACH}" "${HOSTNAMES[${INDEX}]}"
		done
	else
		Error "No SSH Hosts found."
	fi
	FullBar
}

# Command line pre-processor - this section of code is where your functions are called.
shopt -u dotglob

case "${1}" in
		-h|--help|-\?) Help "$@"; exit 0;; # TODO: Enhance this section.
		-s|--scan) VersionDump "$@"; CheckRoot "$@"; Scan "$@"; exit 0;;
		-v|--version) VersionDump "$@"; exit 0;;
		*) Help; exit 0;;
	 esac

