如何在golang上创建sftp客户端而不是ssh连接

时间:2017-07-14 17:04:57

标签: go sftp

家伙!我需要帮助。我尝试在几分钟前创建的现有ssh客户端上创建一个新的sftp连接。我不能。而idk为什么。

这是一段代码:

    config := &ssh.ClientConfig{
        User: *loginArg,
        Auth: []ssh.AuthMethod{
            ssh.Password(*passArg),
        },
        HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
            return nil
        },
    }

    addr := fmt.Sprintf("%s:%d", *ipArg, *portArg)
    client, err := ssh.Dial("tcp", addr, config)
    if err != nil {
        fmt.Printf("Failed to dial: %s", err)
    }
    fmt.Println("Successfully connected to ", *ipArg, ":", *portArg)

    session, err := client.NewSession()
    if err != nil {
        fmt.Printf("Failed to create a new session: %s", err)
    }
    defer session.Close()

    b, err := session.CombinedOutput("/system backup save name=BackUp dont-encrypt=yes") // /system backup save name=BackUp dont-encrypt=yes
    if err != nil {
        fmt.Printf("Failed to send output command: %s", err)
    }
    fmt.Print(string(b))

    //*******************
    var conn *ssh.Client
    sftp, err := sftp.NewClient(conn)
    //sftp, err := sftp.NewClient(conn) //err
    if err != nil {
        log.Fatal(err) //fmt.Printf("Failed to create new sftp-client: %s", err)
    }
    defer sftp.Close()

1 个答案:

答案 0 :(得分:0)

找到答案:只需要客户而不是conn:

client,err:= ssh.Dial(“tcp”,addr,config)

sftp,错误:= sftp.NewClient(客户端)