golang - [求解]使用go开发sshclient 认证失败

查看:1353
本文介绍了golang - [求解]使用go开发sshclient 认证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

代码如下


package main

import (
    "bytes"
    "fmt"
    "log"

    "golang.org/x/crypto/ssh"
)

func sshclientfunc() {

    config := &ssh.ClientConfig{
        User: "user",
        Auth: []ssh.AuthMethod{
            ssh.Password("password"),
        },
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }
    client, err := ssh.Dial("tcp", "hostip:22", config)
    if err != nil {
        log.Fatal("Failed to dial: ", err)
    }

    // Each ClientConn can support multiple interactive sessions,
    // represented by a Session.
    session, err := client.NewSession()
    if err != nil {
        log.Fatal("Failed to create session: ", err)
    }
    defer session.Close()

    // Once a Session is created, you can execute a single command on
    // the remote side using the Run method.
    var b bytes.Buffer
    session.Stdout = &b
    if err := session.Run("/usr/bin/whoami"); err != nil {
        log.Fatal("Failed to run: " + err.Error())
    }
    fmt.Println(b.String())
}


返回错误信息

Failed to dial: ssh: handshake failed: ssh: unable to authenticate,
attempted methods [none], no supported methods remain

解决方案

我测试了你的这段代码,可以正常运行;

我推测你应该是服务器的 sshd 配置文件 /etc/ssh/sshd_config 没有配置密码验证

PasswordAuthentication yes

这篇关于golang - [求解]使用go开发sshclient 认证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆