feat: funciones SSH para infra — conn, check, exec, download, upload, tunnel
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>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user