Skip to content

Instantly share code, notes, and snippets.

@benmcginnis
Created January 11, 2016 16:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmcginnis/de39c37ae4b1456790eb to your computer and use it in GitHub Desktop.
Save benmcginnis/de39c37ae4b1456790eb to your computer and use it in GitHub Desktop.
Golang: Connect to SFTP Server via Keyboard Interactive Password
// much help received from https://github.com/mindreframer/golang-stuff/blame/master/github.com/mitchellh/packer/communicator/ssh/password.go
package main
import (
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func main() {
c := &ssh.ClientConfig{
User: "[username]", // replace this
Auth: []ssh.AuthMethod{
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
// Just send the password back for all questions
answers := make([]string, len(questions))
for i, _ := range answers {
answers[i] = "[password]" // replace this
}
return answers, nil
}),
},
}
connection, err := ssh.Dial("tcp", "[host]:[port]", c) // replace this
if err != nil {
fmt.Println(err)
return
}
server, err := sftp.NewClient(connection)
if err != nil {
fmt.Println(err)
return
}
dir, err := server.ReadDir(".")
if err != nil {
fmt.Println(err)
return
}
for _, fi := range dir {
fmt.Println(fi.Name())
}
}
@AnirudhVyas
Copy link

I just tried keyboard interactive -- i have been busting my brains for a while it works

@AnirudhVyas
Copy link

music to my ears my friend music to my ears ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment