Created
January 11, 2016 16:17
-
-
Save benmcginnis/de39c37ae4b1456790eb to your computer and use it in GitHub Desktop.
Golang: Connect to SFTP Server via Keyboard Interactive Password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()) | |
} | |
} |
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
I just tried keyboard interactive -- i have been busting my brains for a while it works