summaryrefslogtreecommitdiff
path: root/core/modules/ping.sh
blob: b53564a645cf80cdc05104012cc2aee3536b3cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# Module: ping
# Description: Verifica conectividad desde el host remoto hacia un destino específico
# License: GPLv3
# Author: Luis GuLo
# Version: 1.2
# Dependencies: ping, ssh

ping_task() {
  local host="$1"; shift
  declare -A args; for arg in "$@"; do key="${arg%%=*}"; value="${arg#*=}"; args["$key"]="$value"; done

  local count="${args[count]:-2}"
  local timeout="${args[timeout]:-3}"
  local target="${args[target]:-127.0.0.1}"
  local become="${args[become]:-true}"
  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

   # 🌐 Cargar traducciones
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/ping.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then
    while IFS='=' read -r key val; do tr["$key"]="$val"; done < "$trfile"
  else
    echo "⚠️ [ping] Archivo de traducción no encontrado: $trfile"
  fi

  echo "$(render_msg "${tr[start]}" "host=$host" "target=$target")"

  if ssh "$host" "$prefix ping -c $count -W $timeout $target &>/dev/null"; then
    echo "    $(render_msg "${tr[success]}" "host=$host" "target=$target")"
    return 0
  else
    echo "    $(render_msg "${tr[fail]}" "host=$host" "target=$target")"
    return 1
  fi
}

check_dependencies_ping() {
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/ping.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then
    while IFS='=' read -r key val; do tr["$key"]="$val"; done < "$trfile"
  fi

  if ! command -v ssh &> /dev/null || ! command -v ping &> /dev/null; then
    echo "    ${tr[missing_deps]:-❌ [ping] ssh o ping no están disponibles.}"
    return 1
  fi
  echo "    ${tr[deps_ok]:-✅ [ping] ssh y ping disponibles.}"
  return 0
}