#!/usr/bin/env bash
##############################################################
#     Script Para Automatizacion De Kraken Console           #
#           Author: Marian Sanchez                           #
##############################################################

echo "#!/usr/bin/env bash" > /tmp/pass
echo "sudo -S passwd $2 <<EOF
$3
$3
EOF" >> /tmp/pass

if [ -z $1 ] || [ $# -ne 4 ]; then

echo -e "\e[91m PARAMETROS NECESARIOS: nombre_usuario_nuevo contrasena_usuario_nuevo\e[0m"
echo -e "\e[91m MODO DE USO : kraken crear_usuario 1 nombre_inventario nombre_usuario_nuevo contrasena_usuario_nuevo\e[0m"
exit;

fi

ls /home/kraken/clientes/$1 &> /dev/null

if [ $? -ne 0 ]; then

	echo -e "\e[91m ARCHIVO DE INVENTARIO $1 NO EXISTE EN /home/kraken/clientes/\e[0m"
	exit 1;
	
fi

readarray -t arreglo < /home/kraken/clientes/$1

echo -e "\n"
echo -e "\e[93m ========== CREACION USUARIO $2 EN LOS SERVIDORES DEL INVENTARIO: $1 ==============\e[0m"


 for b in ${arreglo[@]}
 do
 
			sshpass -p $4 ssh $b -o StrictHostKeyChecking=no -o ConnectTimeout=5 "exit" &> /dev/null
		
			if [ $? -eq 0 ]; then
			
			
				sshpass -p $4 ssh $b -o ConnectTimeout=5  "cat /etc/passwd | grep $2" &> /dev/null
				
				if [ $? -eq 0 ]; then
				
					echo -e "\e[93m USUARIO $2 YA EXISTE EN EL SERVIDOR $b .. OBVIANDO CREACION\e[0m"
					echo $b >> /tmp/create_user_failed1
					
				else
 
 
					echo $b >> /tmp/user_log
					sshpass -p $4 ssh $b -o ConnectTimeout=5  "echo $4 | sudo -S useradd -m $2 ; exit" &>> /tmp/user_log


					if [ $? -eq 0 ]
					then
				
						sshpass -p $4 scp /tmp/pass $b:/tmp &>>  /dev/null	
						sshpass -p $4 ssh $b -o ConnectTimeout=5 "chmod +x /tmp/pass && echo $4 | sudo -S bash /tmp/pass; rm -f /tmp/pass; exit" &> /dev/null
						
					echo $b >> /tmp/create_user
					echo -e "\e[93m ========== VALIDANDO ACCESO AL SERVIDOR $b CON EL NUEVO USUARIO $2 ==============\e[0m"
				
						sshpass -p $3 ssh $b -l $2 -o ConnectTimeout=5 "exit" &> /dev/null
					
						if [ $? -eq 0 ]; then
						
							echo -e "\e[32m $b OK \e[0m"
						
						else
					
							echo -e "\e[31m $b FAIL \e[0m"
						
						fi
				

					else
						echo -e "\e[31m $b FAIL \e[0m"
                        echo $b >> /tmp/create_user_failed

					fi
				fi
		
	else
			
			echo $b >> /tmp/failed_access
		
		fi
		
done


si=$(cat /tmp/create_user)
no1=$(cat /tmp/create_user_failed1)
total_ok=$(cat /tmp/create_user | wc -l)
cat /tmp/failed_access >> /tmp/create_user_failed
total_nok=$(cat /tmp/create_user_failed | wc -l)
log=$(cat /tmp/user_log)
log1=$(cat /tmp/failed_access)

echo -e "\e[93m ---------------------------------------------------------- \e[0m"
echo -e "\n"

echo -e "\e[32m ========== USUARIO $2 CREADO EN LOS SERVIDORES ==============\e[0m"
echo -e "\e[32m $si \e[0m"
echo -e "\n"

echo -e "\e[31m ========== USUARIO $2 NO CREADO EN LOS SERVIDORES ==============\e[0m"
echo -e "\e[31m $no1 \e[0m"
echo -e "\n"
echo -e "\e[93m Servidores Ok: $total_ok	Servidores Failed: $total_nok   \e[0m"
echo -e "\n\n"
echo -e "\e[31m ========== SERVIDORES CON PROBLEMAS ACCESO (VALIDE PING O CREDENCIALES) ==============\e[0m"
echo -e "\e[31m $log1 \e[0m"
echo -e "\n"
echo -e "\e[93m --------------------- LOGS ------------------------ \e[0m"
echo -e "\e[93m $log \e[0m"

rm -f /tmp/user_log &> /dev/null
rm -f /tmp/create_user &> /dev/null
rm -f /tmp/create_user_failed &> /dev/null
rm -f /tmp/pass &> /dev/null
rm -f /tmp/failed_access &> /dev/null
rm -f /tmp/create_user_failed1 &> /dev/null

exit 0;
