esxlib/pkg/sshutil/commands.go

55 lines
1.2 KiB
Go
Raw Normal View History

2023-06-22 05:16:40 +00:00
package sshutil
import (
"golang.org/x/crypto/ssh"
)
type Auth struct {
Host string
User string
Password string
}
func SshInteractive(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
answers = make([]string, len(questions))
// The second parameter is unused
for n, _ := range questions {
answers[n] = "your_password"
}
return answers, nil
}
func CombinedOutput(auth Auth, cmd string) ([]byte, error) {
sshConfig := &ssh.ClientConfig{
User: auth.User,
Auth: []ssh.AuthMethod{
ssh.KeyboardInteractive(
func(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
answers = make([]string, len(questions))
// The second parameter is unused
for n, _ := range questions {
answers[n] = auth.Password
}
return answers, nil
}),
},
}
sshConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
client, err := ssh.Dial("tcp", auth.Host, sshConfig)
if err != nil {
return nil, err
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
return nil, err
}
defer session.Close()
out, err := session.CombinedOutput(cmd)
return out, err
}