Initial code commit
This commit is contained in:
40
pkg/utils/ssh.go
Normal file
40
pkg/utils/ssh.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user