#!/bin/bash 

function Test_Detect_OS {
	user=$1
	pass_file=$2
	host=$3

	message="Target OS is not detected. Test results:\\\n"
	# Ping test
	ping -c 3 "$host" > /dev/null 2>&1
	ping_rc=$?
	if [[ $ping_rc != 0 ]]
	then
		message="${message} - [FAIL]Ping test failed.\\\n"
	else
		message="${message} - [OK]Ping test succeeded.\\\n"
	fi

	# SSH Test
	ssh_enabled_port=0
	for port in $ssh_ports
	do
		Test_Port "$host" "$port"
		if [[ $? == 0 ]]
		then
			ssh_enabled_port=$port
			break
		fi
	done
	if [[ $ssh_enabled_port != 0 ]]
	then
		message="${message} - [OK]SSH Port $ssh_enabled_port Enabled.\\\n"
		Invoke_SSH_Command "$user" "$pass_file" "$host" "$ssh_enabled_port" "hostname" > /dev/null 2>&1
		if [[ $? == 0 ]]
		then
			message="${message} - [OK]SSH User Credential is correct.\\\n"
		else
			message="${message} - [ERROR]User Credential is not correct for this device.\\\n"
		fi
	else
		message="${message} - [FAIL]SSH Disabled.\\\n"
	fi

	# Admin Share Test
	Test_Port "$host" 445
	if [[ $? == 0 ]]
	then
		message="${message} - [OK]SMB Port 445 Enabled.\\\n"
		Test_Admin_Share "$user" "$pass_file" "$host"
        	if [[ $? == 0 ]]
	        then
        	        message="${message} - [OK]Admin Share is available.\\\n"
	        else
        	        message="${message} - [FAIL]Admin Share is not available.\\\n"
	        fi
	else
		message="${message} - [FAIL]SMB Port 445 Disabled.\\\n"
	fi

	# WMI Tests
	wmi_test_result="ND"
	Test_Port "$host" 135
	if [[ $? == 0 ]]
	then
		message="${message} - [OK]WMI Port 135 Enabled.\\\n"
		{ wmi_test_output=$( (Test_RPC "$user" "$pass_file" "$host") 2>&1 1>&3-) ;} 3>&1
		wmi_test_result=$?
		if [[ $wmi_test_result == 0 ]]
		then
				message="${message} - [OK]WMI User Credential is correct.\\\n"
		else
				message="${message} - [FAIL]WMI Disabled - $wmi_test_output.\\\n"
		fi
	else
		message="${message} - [FAIL]WMI Port 135 Disabled.\\\n"
	fi

	# Summary
	if [[ $ssh_enabled_port == 0 && $wmi_test_result != 0 ]]
	then
		message="${message} - [FAIL]Supported access methods are not available(SSH,WMI).\\\n"
	fi

	echo -e $message
}
