drfqfuhej48511519 2018-11-11 21:29
浏览 177
已采纳

在Go中通过SSH发送文件

I found this answer before posting this question but the answer is not clear to me.

Here is the code of the answer:

conn, err := ssh.Dial("tcp", hostname+":22", config)
if err != nil {
    return err
}

session, err := conn.NewSession()
if err != nil {
    return err
}
defer session.Close()

r, err := session.StdoutPipe()
if err != nil {
    return err
}

name := fmt.Sprintf("%s/backup_folder_%v.tar.gz", path, time.Now().Unix())
file, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
    return err
}
defer file.Close()

if err := session.Start(cmd); err != nil {
    return err
}

n, err := io.Copy(file, r)
if err != nil {
    return err
}

if err := session.Wait(); err != nil {
    return err
}

return nil

I don't understand relation between the cmd variable and io.Copy, where and how does it know which file to copy. I like the idea of using io.Copy but i do not know how to create the file via ssh and start sending content to it using io.Copy.

展开全部

  • 写回答

1条回答 默认 最新

  • dtot74529 2018-11-12 00:52
    关注

    Here is a minimal example on how to use Go as an scp client:

    config := &ssh.ClientConfig{
        User: "user",
        Auth: []ssh.AuthMethod{
            ssh.Password("pass"),
        },
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }
    
    client, _ := ssh.Dial("tcp", "remotehost:22", config)
    defer client.Close()
    
    session, _ := client.NewSession()
    defer session.Close()
    
    file, _ := os.Open("filetocopy")
    defer file.Close()
    stat, _ := file.Stat()
    
    wg := sync.WaitGroup{}
    wg.Add(1)
    
    go func() {
        hostIn, _ := session.StdinPipe()
        defer hostIn.Close()
        fmt.Fprintf(hostIn, "C0664 %d %s
    ", stat.Size(), "filecopyname")
        io.Copy(hostIn, file)
        fmt.Fprint(hostIn, "\x00")
        wg.Done()
    }()
    
    session.Run("/usr/bin/scp -t /remotedirectory/")
    wg.Wait()
    

    Note that I have ignored all errors only for conciseness.

    1. session.StdinPipe() will create a writable pipe for the remote host.
    2. fmt.Fprintf(... "C0664 ...") will signal the start of a file with 0664 permission, stat.Size() size and the remote filename filecopyname.
    3. io.Copy(hostIn, file) will write the contents of file into hostIn.
    4. fmt.Fprint(hostIn, "\x00") will signal the end of the file.
    5. session.Run("/usr/bin/scp -qt /remotedirectory/") will run the scp command.

    Edit: Added waitgroup per OP's request

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部