add ssh pkg

This commit is contained in:
William Banfield
2022-11-30 12:41:38 -05:00
parent db7b4a0e32
commit e009a2c7ee

33
test/e2e/pkg/ssh/ssh.go Normal file
View File

@@ -0,0 +1,33 @@
package ssh
import (
"os"
"path/filepath"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
func Exec(cfg *ssh.ClientConfig, addr, cmd string) error {
c, err := ssh.Dial("tcp", addr, cfg)
if err != nil {
return err
}
s, err := c.NewSession()
defer s.Close()
err = s.Run(cmd)
if err != nil {
return err
}
return nil
}
func NewClientConfig(key string) (*ssh.ClientConfig, error) {
hkc, err := knownhosts.New(filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts"))
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
HostKeyCallback: hkc,
}, nil
}