LoginSignup
19
16

More than 5 years have passed since last update.

golangでscp

Posted at

ググると出てくるサンプルがよくわからないので調べてみた。

https://gist.github.com/jedy/3357393
よくわからないけど、sshパッケージ使ってなぜかリモート側のscpコマンドを実行している・・これでいいのか?

if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {

みたことのない-tオプションとは?
man scp してみよう
http://www.openbsd.org/cgi-bin/man.cgi?query=scp
あれ?記載がない。

よく見ると先ほどのサンプルコードの先頭に URLがあったので
https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works
読んでみる

どうやら隠しオプションでscpプロトコルはこれを使っているようです。
-tで起動して、標準入力からファイル情報とファイル内容を受け取るようです。
具体的には、

go func() {
        w, _ := session.StdinPipe()
        defer w.Close()
        content := "123456789\n"
        fmt.Fprintln(w, "C0644", len(content), "testfile")
        fmt.Fprint(w, content)
        fmt.Fprint(w, "\x00") // 传输以\x00结束
    }()

この部分ですね。

パーミッション情報とサイズとファイル名を送って
ファイルの内容を送って最後に\x00で終わる

なるほど、
簡単だけどちょっと面倒なんで上手くラップしたいところ。

19
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
16