I have been struggling with this for around a week now. I have tried an scp client for golang but was informed that the code was broken, and the contributed "fixed" code didn't work as well. Anyway, i'm giving up on file transferring and decided to just use ssh to create files and save on the remote machine.
I successfully ran the ssh client in golang before going into scp route so this may be a good solution.
In the ssh client it just executed the "ls" command and I am aware that there is a "touch" command to create a file.
var b bytes.Buffer
session.Stdout = &b
if err := session.Run("ls"); err != nil {
panic("Failed to run: " + err.Error())
}
fmt.Println(b.String())
Ultimately what I would like to achieve is read a local file maybe through os.Open(localfile)
get it's contents then create a new file on the remote machine with the "touch" command then edit it and stick in the contents from the local file earlier. Or maybe there is a command that will make a new file and prepare it's contents already?
This looked promising, although this code gives me an error but from my observation, this would create a file called "testfile" with content of "123456789
" then I think upload it using session.Run("/usr/bin/scp -qrt ./");
?
go func() {
w, _ := session.StdinPipe()
defer w.Close()
content := "123456789
"
fmt.Fprintln(w, "C0644", len(content), "testfile")
fmt.Fprint(w, content)
fmt.Fprint(w, "\x00") // 传输以\x00结束
}()
if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
panic("Failed to run: " + err.Error())
}
The above code can be found in my previous question about scp in golang
I'm not sure if this is what the code does, as I can't run it because of the error. I've tried to find out what the /usr/bin/scp -qrt ./
command does although couldn't exactly understand it.
I've been on this project for a week now and is very frustrating. Any help would be greatly appreciated. Thanks in advance.