packer-cn/communicator/ssh/keyboard_interactive.go

34 lines
768 B
Go
Raw Normal View History

2020-03-05 22:22:48 -05:00
package ssh
import (
2020-03-13 01:01:11 -04:00
"io"
2020-03-08 22:54:53 -04:00
"log"
2020-03-12 22:22:13 -04:00
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
2020-03-05 22:22:48 -05:00
)
2020-03-13 01:01:11 -04:00
func KeyboardInteractive(c io.ReadWriter) ssh.KeyboardInteractiveChallenge {
t := terminal.NewTerminal(c, "")
2020-03-05 22:22:48 -05:00
return func(user, instruction string, questions []string, echos []bool) ([]string, error) {
if len(questions) == 0 {
return []string{}, nil
}
2020-03-08 22:58:53 -04:00
log.Printf("[INFO] -- User: %s", user)
log.Printf("[INFO] -- Instructions: %s", instruction)
2020-03-05 22:22:48 -05:00
for i, question := range questions {
2020-03-08 22:58:53 -04:00
log.Printf("[INFO] -- Question %d: %s", i+1, question)
2020-03-05 22:22:48 -05:00
}
answers := make([]string, len(questions))
for i := range questions {
2020-03-13 01:01:11 -04:00
s, err := t.ReadPassword("")
2020-03-05 22:22:48 -05:00
if err != nil {
return nil, err
}
answers[i] = string(s)
}
return answers, nil
}
}