I've been trying to get a simple little scp implementation going that tries to download a specific file.
As I understand it, I can provide an interactive authenticator that will be used to prompt the user for login information, if required.
However, when I use this, my Challenge implementation is simply not called. I'm on Windows, if that makes a difference.
Here's the code. Please forgive the quality - this was something I threw together in about 15 minutes.
I would appreciate any pointers in the right direction.
Cheers,
Carl
package main
import (
"io"
)
type interactive_authenticator string
func (f interactive_authenticator) Challenge(user, instruction string, questions []string, echos []bool) ([]string, error) {
//Yes, I know about the RFC, but I just want to see what happens here.
println("trying to get password from user")
answers := []string{}
print(instruction)
print(user)
print(echos)
for _, q := range questions {
println(q)
}
answers = append(answers, string(gopass.GetPasswd()))
return answers, nil
}
func get_interactive_authenticator() (i interactive_authenticator) {
return i
}
func doscp_custom_port(host, port, username, passwd, remote_file, local_file string) error {
// Dial code is taken from the ssh package example
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyboardInteractive(get_interactive_authenticator()),
},
}
client, err := ssh.Dial("tcp", host+":"+port, config)
if err != nil {
println("Unable to connect" + err.Error())
return err
}
session, err := client.NewSession()
if err != nil {
println("Failed to create session: " + err.Error())
return err
}
defer session.Close()
go func() {
r, _ := session.StdoutPipe()
if w, err := open_file(local_file); err == nil {
defer w.Close()
io.Copy(w, r)
}
}()
if err := session.Run("`which cat` " + remote_file); err != nil {
println("Could not retrieve file. Reason:" + err.Error())
return err
}
return nil
}
func main() {
if err := doscp_custom_port("localhost", "22", "", "~/test.txt", "test.txt"); err == nil{
println("File copied successfully")
} else {
println("Could not retrieve the file")
}
}