From 533e79ba959143f0459431a486bfb85c56c72ddc Mon Sep 17 00:00:00 2001 From: luisgulo Date: Fri, 24 Oct 2025 18:01:10 +0200 Subject: Releasing code version 1.8.0 --- core/utils/shflow-ssh-init.sh | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 core/utils/shflow-ssh-init.sh (limited to 'core/utils/shflow-ssh-init.sh') diff --git a/core/utils/shflow-ssh-init.sh b/core/utils/shflow-ssh-init.sh new file mode 100755 index 0000000..26d5e70 --- /dev/null +++ b/core/utils/shflow-ssh-init.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# Utility: shflow-ssh-init +# Description: Inicializa acceso SSH sin contraseña en los hosts del inventario +# Author: Luis GuLo +# Version: 0.2.0 + +set -euo pipefail + +# 📁 Rutas defensivas +PROJECT_ROOT="${SHFLOW_HOME:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" +INVENTORY="$PROJECT_ROOT/core/inventory/hosts.yaml" +TIMEOUT=5 +USER="${USER:-$(whoami)}" +KEY="${KEY:-$HOME/.ssh/id_rsa.pub}" + +# 🧩 Cargar render_msg si no está disponible +COMMON_LIB="$PROJECT_ROOT/core/lib/translate_msg.sh" +if ! declare -f render_msg &>/dev/null; then + [[ -f "$COMMON_LIB" ]] && source "$COMMON_LIB" +fi + +export SHFLOW_LANG="${SHFLOW_LANG:-es}" +# 🌐 Cargar traducciones +lang="${SHFLOW_LANG:-es}" + +trfile="$PROJECT_ROOT/core/utils/shflow-ssh-init.tr.${lang}" +declare -A tr +if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi + +echo "$(render_msg "${tr[start]}" "user=$USER")" +echo "$(render_msg "${tr[inventory]}" "path=$INVENTORY")" +echo "$(render_msg "${tr[key]}" "key=$KEY")" +echo "" + +# 🧪 Validar dependencias +for cmd in yq ssh ssh-copy-id; do + if ! command -v "$cmd" &>/dev/null; then + echo "$(render_msg "${tr[missing_dep]}" "cmd=$cmd")" + exit 1 + fi +done + +# 🔁 Extraer hosts +HOSTS=() +HOSTS_RAW=$(yq ".all.hosts | keys | .[]" "$INVENTORY") +[ -z "$HOSTS_RAW" ] && echo "${tr[no_hosts]:-❌ No se encontraron hosts en el inventario.}" && exit 1 + +while IFS= read -r line; do + HOSTS+=("$(echo "$line" | sed 's/^\"\(.*\)\"$/\1/')") # Eliminar comillas +done <<< "$HOSTS_RAW" + +# 🔍 Evaluar cada host +for host in "${HOSTS[@]}"; do + IP=$(yq -r ".all.hosts.\"$host\".ansible_host" "$INVENTORY") + [[ "$IP" == "null" || -z "$IP" ]] && echo "$(render_msg "${tr[missing_ip]}" "host=$host")" && continue + + echo "$(render_msg "${tr[checking]}" "host=$host" "ip=$IP")" + + if ssh -o BatchMode=yes -o ConnectTimeout=$TIMEOUT "$USER@$IP" 'true' &>/dev/null; then + echo "${tr[skip]:- 🔁 Inicialización SSH no es necesaria}" + continue + fi + + echo "$(render_msg "${tr[copying]}" "user=$USER" "ip=$IP")" + if ssh-copy-id -i "$KEY" "$USER@$IP"; then + echo "${tr[success]:- ✅ Clave pública instalada correctamente}" + else + echo "${tr[fail]:- ❌ Fallo al instalar clave pública}" + fi + + echo "" +done + +echo "${tr[done]:-✅ Proceso de inicialización SSH completado}" -- cgit v1.2.3