#!/bin/bash 

function Detect_OS {
	user=$1
	pass_file=$2
	host=$3
	ssh_ports=$4

	retval="error"

	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
		# Add new host to known hosts
		ssh-keygen -R "$host" >/dev/null

		# Copy certificate
		sshpass -f $pass_file ssh-copy-id -p $ssh_enabled_port -f -o "StrictHostKeyChecking=no" "$user@$host" >/dev/null

		# Invoke command
		output=$(Invoke_SSH_Command "$user" "$pass_file" "$host" "$ssh_enabled_port" "uname")
		if [[ $? != 0 ]]
		then
			Invoke_SSH_Command "$user" "$pass_file" "$host" "$ssh_enabled_port" "cmd /c" >/dev/null
			if [[ $? == 0 ]]
			then
				retval="windows"
			fi
		else
			$(echo $output | grep -i "windows" >/dev/null) && retval="windows"
			$(echo $output | grep -i "linux" >/dev/null) && retval="linux"
		fi
	fi

	Test_Port "$host" 135
	if [[ $? == 0 ]]
	then
		wmi_out=$(Test_RPC "$user" "$pass_file" "$host")
		if [[ $? == 0 ]]
		then
			retval="windows"
		fi
	fi

	echo $retval
}
