Problems with scp command on go ssh server

Hi there,

I have written a simple sshd which can handle shell sessions and the execution of commands.
But i have a problem with the scp command.

If i try to copy something from a client computer to the server with my simplesshd running it creates the file at the correct location but without any content in it. The file is 0kb. The same happens if i try to copy a directory recusively.

This is how i execute the scp command, which i run on the client:

scp -P 2200 test.file user@localhost:/home/user/

Here is the function that handles the exec command within my simplesshd.go:

func executeCommand(name string, args []string, c ssh.Channel) {
    cmd := exec.Command(name, args...)

    cmdChannel, err := pty.Start(cmd)
    if err != nil {
        log.Printf("Could not start pty %s", err)
        c.Close()
        return
    }

    close := func() {
         c.Close()
    }

    var once sync.Once
    go func() {
        io.Copy(c, cmdChannel)
        once.Do(close)
    }()
    go func() {
        io.Copy(cmdChannel, c)
        once.Do(close)
    }()
}

pty.Start is a function from the package GitHub - kr/pty: PTY interface for Go

Here is a gist of a simplified version of simplesshd.go: gist

How can i fix/handle the scp command within my simplesshd to work as expected?

Thanks in advance!

I’m not sure if you know this, perhaps you do, but you need to give the scp binary on the host you are copying to access to the input/output streams of the connection on stdin and stdout of the process. See more information here. You essentially “hand over” the input/output streams of the connection the the scp process during a file transfer.

Here’s an idea of the command that is attempted to run on the “remote host” side where you are sending the file to.

Good luck.

Thank you. I got it to work. I updated the gist for reference.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.