Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SSH在Go中:无法进行身份验证,尝试的方法[none],没有支持的方法保留_Go_Ssh - Fatal编程技术网

SSH在Go中:无法进行身份验证,尝试的方法[none],没有支持的方法保留

SSH在Go中:无法进行身份验证,尝试的方法[none],没有支持的方法保留,go,ssh,Go,Ssh,我尝试使用SSH和Go连接到我的一个虚拟机。 如果我这样做的话,它通过命令行工作得非常好: ssh root@my_host 我输入了密码,一切正常。 我试着在围棋中做,下面是我的代码: package main import ( "golang.org/x/crypto/ssh" "fmt" ) func connectViaSsh(user, host string, password string) (*ssh.Client, *ssh.Session) {

我尝试使用SSH和Go连接到我的一个虚拟机。 如果我这样做的话,它通过命令行工作得非常好:

ssh root@my_host
我输入了密码,一切正常。 我试着在围棋中做,下面是我的代码:

package main

import (
    "golang.org/x/crypto/ssh"
    "fmt"
)

func connectViaSsh(user, host string, password string) (*ssh.Client, *ssh.Session) {
    config := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{ssh.Password(password)},
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }

    client, err := ssh.Dial("tcp", host, config)
    fmt.Println(err)

    session, err := client.NewSession()
    fmt.Println(err)

    return client, session
}


func main() {
    client, _ := connectViaSsh("root", "host:22", "password")
    client.Close()
}
如果我运行它,它将返回一个错误:

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

有人知道是什么导致了这样的错误吗。在Python和shell中使用paramiko可以正常工作,但在Go中失败。我缺少什么吗?

正如@JimB和@putu所指出的,我的服务器没有启用密码验证

验证我是否使用verbose选项运行ssh,并返回所有支持的身份验证方法。 就我而言,结果是:

debug1 : Authentications that can continue: publickey,keyboard-interactive,hostbased
所以我有两个选择,要么在服务器上启用密码身份验证,要么使用其他方法进行身份验证

要启用密码身份验证,请连接到服务器并打开sshd config文件,如下所示:

vi /etc/ssh/sshd_config
查找语句:PasswordAuthentication no

将其更改为“是”,保存更改并重新启动sshd服务:
service ssh restart

之后,密码验证方法开始按预期工作

或者也可以使用其他方法,我决定尝试键盘交互,这是一个用户在使用ssh通过终端连接时通常使用的方法

下面是一段代码片段,用于在远程服务器询问密码问题后发送密码:

package main
import (
    "bytes"
    "golang.org/x/crypto/ssh"
    "fmt"
)

func connectViaSsh(user, host string, password string) (*ssh.Client, *ssh.Session) {
    config := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            ssh.KeyboardInteractive(SshInteractive),
        },
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }
    client, err := ssh.Dial("tcp", host, config)
    fmt.Println(err)
    session, err := client.NewSession()
    fmt.Println(err)

    return client, session
}

func SshInteractive(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
    answers = make([]string, len(questions))
    // The second parameter is unused
    for n, _ := range questions {
        answers[n] = "your_password"
    }

    return answers, nil
}

func main() {
    var b bytes.Buffer
    client, session := connectViaSsh("root", "host:22", "password")

    session.Stdout = &b
    session.Run("ls")
    fmt.Println(b.String())

    client.Close()
}

在我的例子中,服务器只会问一个问题,即密码,如果您的服务器问的问题超过这个问题,您需要构建一个完整的答案链来反馈。

您的服务器可能不使用“密码”身份验证。使用ssh命令连接到详细的输出,并查看身份验证机制是什么。除了Jim的评论,请看我不理解这个赏金。你回答了这个问题并提供了一份赏金(说明一个答案是模范的,值得赏金),但你不能给自己赏金…@icza我想有人会给出一个更好的答案或更完整的答案,我会给他/她赏金