2023-11-18 16:14:39 +03:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
2023-11-19 17:03:12 +03:00
|
|
|
"strings"
|
2023-11-18 16:14:39 +03:00
|
|
|
|
2023-11-29 18:52:59 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/pkg/smarttypes"
|
2023-11-18 16:14:39 +03:00
|
|
|
"github.com/kevinburke/ssh_config"
|
|
|
|
)
|
|
|
|
|
2023-11-19 17:03:12 +03:00
|
|
|
type hostConfig struct {
|
|
|
|
cfg *ssh_config.Config
|
|
|
|
host string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *hostConfig) Get(key string) (string, error) {
|
|
|
|
val, err := c.cfg.Get(c.host, key)
|
|
|
|
return strings.TrimSpace(val), err
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:52:59 +03:00
|
|
|
func parseSSHConfig(filePath smarttypes.Path) (*ssh_config.Config, error) {
|
|
|
|
fileName, err := filePath.File()
|
2023-11-18 16:14:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
file, err := os.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ssh_config.Decode(bytes.NewReader(file))
|
|
|
|
}
|