• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    go语言使用scp的方法实例分析

    本文实例讲述了go语言使用scp的方法。分享给大家供大家参考。具体如下:

    复制代码 代码如下:
    package main
    import (
        "code.google.com/p/go.crypto/ssh"
        "crypto"
        "crypto/rsa"
        "crypto/x509"
        "encoding/pem"
        "fmt"
        "io"
    )
    const privateKey = `content of id_rsa`
    type keychain struct {
        key *rsa.PrivateKey
    }
    func (k *keychain) Key(i int) (interface{}, error) {
        if i != 0 {
            return nil, nil
        }
        return k.key.PublicKey, nil
    }
    func (k *keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
        hashFunc := crypto.SHA1
        h := hashFunc.New()
        h.Write(data)
        digest := h.Sum(nil)
        return rsa.SignPKCS1v15(rand, k.key, hashFunc, digest)
    }
    func main() {
        block, _ := pem.Decode([]byte(privateKey))
        rsakey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
        clientKey := keychain{rsakey}
        clientConfig := ssh.ClientConfig{
            User: "wuhao",
            Auth: []ssh.ClientAuth{
                ssh.ClientAuthKeyring(clientKey),
            },
        }
        client, err := ssh.Dial("tcp", "127.0.0.1:22", clientConfig)
        if err != nil {
            panic("Failed to dial: " + err.Error())
        }
        session, err := client.NewSession()
        if err != nil {
            panic("Failed to create session: " + err.Error())
        }
        defer session.Close()
        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结束
        }()
        if err := session.Run("/usr/bin/scp -qrt ./"); err != nil {
            panic("Failed to run: " + err.Error())
        }
    }

    希望本文所述对大家的Go语言程序设计有所帮助。

    您可能感兴趣的文章:
    • go语言中时间戳格式化的方法
    • Go语言计算指定年月天数的方法
    • Go语言计算两个经度和纬度之间距离的方法
    • go语言获取系统盘符的方法
    • go语言实现的memcache协议服务的方法
    • go语言使用pipe读取子进程标准输出的方法
    • go语言通过管道连接两个命令行进程的方法
    • go语言使用RC4加密的方法
    • go语言计算两个时间的时间差方法
    上一篇:深入解析快速排序算法的原理及其Go语言版实现
    下一篇:go语言获取系统盘符的方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    go语言使用scp的方法实例分析 语言,使用,scp,的,方法,实例分析,