560cbf280e
Conjunto completo de funciones SSH para operaciones remotas: conexión, verificación de host, ejecución de comandos, transferencia de archivos (upload/download) y gestión de túneles. Incluye tipo SSHConn y tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package infra
|
|
|
|
import "fmt"
|
|
|
|
// SSHConn parametros de conexion SSH reutilizables.
|
|
type SSHConn struct {
|
|
Host string // Hostname o IP del servidor remoto
|
|
Port int // Puerto SSH (0 usa el default 22)
|
|
User string // Usuario remoto
|
|
KeyPath string // Ruta a clave privada (vacio usa ssh-agent o default)
|
|
}
|
|
|
|
// sshArgs construye los argumentos comunes de ssh/scp a partir de SSHConn.
|
|
func (c SSHConn) sshArgs() []string {
|
|
var args []string
|
|
port := c.Port
|
|
if port == 0 {
|
|
port = 22
|
|
}
|
|
args = append(args, "-o", "StrictHostKeyChecking=accept-new")
|
|
args = append(args, "-p", fmt.Sprintf("%d", port))
|
|
if c.KeyPath != "" {
|
|
args = append(args, "-i", c.KeyPath)
|
|
}
|
|
return args
|
|
}
|
|
|
|
// scpArgs construye los argumentos comunes de scp a partir de SSHConn.
|
|
func (c SSHConn) scpArgs() []string {
|
|
var args []string
|
|
port := c.Port
|
|
if port == 0 {
|
|
port = 22
|
|
}
|
|
args = append(args, "-o", "StrictHostKeyChecking=accept-new")
|
|
args = append(args, "-P", fmt.Sprintf("%d", port))
|
|
if c.KeyPath != "" {
|
|
args = append(args, "-i", c.KeyPath)
|
|
}
|
|
return args
|
|
}
|
|
|
|
// destination retorna user@host.
|
|
func (c SSHConn) destination() string {
|
|
if c.User != "" {
|
|
return c.User + "@" + c.Host
|
|
}
|
|
return c.Host
|
|
}
|