Files
sslh-multiplex-lab/pkg/utils/ssh.go
2026-01-29 00:03:02 +00:00

41 lines
789 B
Go

package utils
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
)
func RemoveSSHKnownHost(host string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get user home directory: %w", err)
}
knownHostsPath := filepath.Join(homeDir, ".ssh", "known_hosts")
if _, err := os.Stat(knownHostsPath); os.IsNotExist(err) {
return nil
}
cmd := exec.Command("ssh-keygen", "-R", host)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to remove host key: %w", err)
}
return nil
}
func GetSSHUser() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", fmt.Errorf("failed to get current user: %w", err)
}
return currentUser.Username, nil
}