95 lines
2.7 KiB
Ruby
95 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# adn/tools/core/nodos_info.rb — Utilidades para extracción de metadatos de fichas de nodos
|
|
# ======================================================================================
|
|
|
|
require_relative 'constants'
|
|
|
|
module ADN
|
|
module NodosInfo
|
|
def self.extraer_metadata(nodo_nombre)
|
|
ruta = File.join(ADN::NODOS_DIR, "#{nodo_nombre}.md")
|
|
return nil unless File.exist?(ruta)
|
|
|
|
contenido = File.read(ruta, encoding: 'UTF-8')
|
|
{
|
|
nombre: nodo_nombre,
|
|
ip: detectar_ip(contenido),
|
|
host: detectar_host(contenido),
|
|
so: detectar_so(contenido),
|
|
vmid: detectar_vmid(contenido),
|
|
puerto_ssh: detectar_puerto_ssh(contenido) || 22,
|
|
usuario: detectar_usuario(contenido)
|
|
}
|
|
end
|
|
|
|
def self.detectar_usuario(contenido)
|
|
# | SSH | user@domain@ip:7022 | o user@ip:7022 o user@domain:7022
|
|
if contenido =~ /(?:SSH|Acceso)\*\*?:?\s*`?([a-zA-Z0-9_\-\.\@]+)/i
|
|
full = $1
|
|
if full.include?('@')
|
|
parts = full.split('@')
|
|
# Si la última parte es una IP, el usuario es el resto
|
|
if parts.last =~ /^\d{1,3}(\.\d{1,3}){3}/
|
|
return parts[0...-1].join('@')
|
|
else
|
|
# Probablemente sea user@domain or user@domain:port
|
|
return full.split(':')[0]
|
|
end
|
|
end
|
|
return full
|
|
end
|
|
nil
|
|
end
|
|
|
|
def self.detectar_ip(contenido)
|
|
# | IP / Ubicación | 10.0.10.10 |
|
|
# | IP | 10.0.100.11/24 |
|
|
if contenido =~ /\|\s*IP\s*(?:\/\s*Ubicación)?\s*\|\s*`?([\d\.]+)/i
|
|
return $1
|
|
end
|
|
# - **IP**: `10.0.10.200`
|
|
if contenido =~ /IP\*\*:\s*`?([\d\.]+)/i
|
|
return $1
|
|
end
|
|
nil
|
|
end
|
|
|
|
def self.detectar_host(contenido)
|
|
if contenido =~ /\*\*(?:Padre\/Host|Host Anfitrión|Anfitrión|Host)\*\*\s*[:\|]?\s*`?([a-zA-Z0-9\-]+)`?/i
|
|
return $1
|
|
end
|
|
nil
|
|
end
|
|
|
|
def self.detectar_so(contenido)
|
|
# | Sistema Operativo | ... | o - **Sistema Operativo**: ...
|
|
if contenido =~ /(?:\||\-)\s*\*\*?(?:Sistema Operativo|SO|OS)\*\*?\s*[:\|]\s*([^\|\n]+)/i
|
|
so_raw = $1.strip
|
|
return :windows if so_raw =~ /windows|server|win/i
|
|
return :linux if so_raw =~ /linux|ubuntu|debian|centos|rocky|proxmox/i
|
|
end
|
|
:linux # Default
|
|
end
|
|
|
|
def self.detectar_vmid(contenido)
|
|
if contenido =~ /(?:VM ID|vmid|VMID)\*\*?:?\s*`?(\d+)`?/i
|
|
return $1
|
|
end
|
|
nil
|
|
end
|
|
|
|
def self.detectar_puerto_ssh(contenido)
|
|
# | SSH | user@ip:7022 |
|
|
if contenido =~ /(?:SSH|Puerto|Puerto SSH)\*\*?:?\s*.*:(\d+)/i
|
|
return $1.to_i
|
|
end
|
|
# (Puerto 7022)
|
|
if contenido =~ /\(Puerto\s+(\d+)\)/i
|
|
return $1.to_i
|
|
end
|
|
nil
|
|
end
|
|
end
|
|
end
|