2020-03-05 22:22:48 -05:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2020-03-06 01:23:06 -05:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2020-03-08 22:54:53 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-03-05 22:22:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func KeyboardInteractive() ssh.KeyboardInteractiveChallenge {
|
|
|
|
return func(user, instruction string, questions []string, echos []bool) ([]string, error) {
|
|
|
|
if len(questions) == 0 {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("-- User: %s", user)
|
|
|
|
log.Printf("-- Instructions: %s", instruction)
|
|
|
|
for i, question := range questions {
|
|
|
|
log.Printf("-- Question %d: %s", i+1, question)
|
|
|
|
}
|
|
|
|
answers := make([]string, len(questions))
|
|
|
|
for i := range questions {
|
2020-03-08 22:54:53 -04:00
|
|
|
var fd int
|
|
|
|
if terminal.IsTerminal(int(os.Stdin.Fd())) {
|
|
|
|
fd = int(os.Stdin.Fd())
|
|
|
|
} else {
|
|
|
|
tty, err := os.Open("/dev/tty")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tty.Close()
|
|
|
|
fd = int(tty.Fd())
|
|
|
|
}
|
|
|
|
s, err := terminal.ReadPassword(fd)
|
2020-03-05 22:22:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
answers[i] = string(s)
|
|
|
|
}
|
|
|
|
return answers, nil
|
|
|
|
}
|
|
|
|
}
|