Add an 'address' field to devices. Can be hostname, ipv4 or ipv6

This commit is contained in:
Pim van Pelt
2025-07-06 18:17:32 +02:00
parent 4a95221732
commit d212abcc87
5 changed files with 180 additions and 18 deletions

View File

@ -20,6 +20,7 @@ import (
// RouterBackup handles SSH connections and command execution
type RouterBackup struct {
hostname string
address string
username string
password string
keyFile string
@ -28,9 +29,10 @@ type RouterBackup struct {
}
// NewRouterBackup creates a new RouterBackup instance
func NewRouterBackup(hostname, username, password, keyFile string, port int) *RouterBackup {
func NewRouterBackup(hostname, address, username, password, keyFile string, port int) *RouterBackup {
return &RouterBackup{
hostname: hostname,
address: address,
username: username,
password: password,
keyFile: keyFile,
@ -38,12 +40,32 @@ func NewRouterBackup(hostname, username, password, keyFile string, port int) *Ro
}
}
// isIPv6 checks if the given address is an IPv6 address
func isIPv6(address string) bool {
ip := net.ParseIP(address)
return ip != nil && ip.To4() == nil
}
// getNetworkType determines the appropriate network type based on the target address
func getNetworkType(address string) string {
if isIPv6(address) {
return "tcp6"
}
return "tcp4"
}
// Connect establishes SSH connection to the router
func (rb *RouterBackup) Connect() error {
// Get SSH config values for this host
hostname := ssh_config.Get(rb.hostname, "Hostname")
if hostname == "" {
hostname = rb.hostname
// Determine the target address - use explicit address if provided, otherwise use hostname
var targetHost string
if rb.address != "" {
targetHost = rb.address
} else {
// Get SSH config values for this host
targetHost = ssh_config.Get(rb.hostname, "Hostname")
if targetHost == "" {
targetHost = rb.hostname
}
}
portStr := ssh_config.Get(rb.hostname, "Port")
@ -144,14 +166,21 @@ func (rb *RouterBackup) Connect() error {
return fmt.Errorf("no authentication method available")
}
address := fmt.Sprintf("%s:%d", hostname, port)
client, err := ssh.Dial("tcp4", address, config)
// Format address properly for IPv6
var address string
if isIPv6(targetHost) {
address = fmt.Sprintf("[%s]:%d", targetHost, port)
} else {
address = fmt.Sprintf("%s:%d", targetHost, port)
}
networkType := getNetworkType(targetHost)
client, err := ssh.Dial(networkType, address, config)
if err != nil {
return fmt.Errorf("failed to connect to %s: %v", hostname, err)
return fmt.Errorf("failed to connect to %s: %v", targetHost, err)
}
rb.client = client
fmt.Printf("Successfully connected to %s\n", hostname)
fmt.Printf("Successfully connected to %s\n", targetHost)
return nil
}
@ -215,10 +244,10 @@ func (rb *RouterBackup) BackupCommands(commands []string, outputDir string) erro
successCount++
}
fmt.Printf("Summary: %d/%d commands successful\n", successCount, len(commands))
if successCount > 0 {
fmt.Printf("Output saved to %s\n", filepath)
}
fmt.Printf("Summary: %d/%d commands successful\n", successCount, len(commands))
return nil
}