如何增加 golang.org/x/crypto/ssh 的详细程度?

huangapple go评论80阅读模式
英文:

how to increase golang.org/x/crypto/ssh verbosity

问题

我想使用golang.org/x/crypto/ssh编写一个小程序,能够读取与ssh -v输出相当的内容。虽然我能够通过ssh包连接到我的服务器,但我不知道如何启用详细输出或获取其中的所需信息。由于我对golang还很陌生,甚至无法确定是否可能实现。

以下是我目前的代码:

package main

import (
	"fmt"

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

type SSHClient struct {
	Config *ssh.ClientConfig
	Host   string
	Port   int
}

func main() {
	sshConfig := &ssh.ClientConfig{
		User: "your_user_name",
		Auth: []ssh.AuthMethod{
			ssh.Password("your_password"),
		},
	}

	client := &SSHClient{
		Config: sshConfig,
		Host:   "example.com",
		Port:   22,
	}

	client.test()
	_, err := ssh.Dial("tcp", "example.com:22", sshConfig)
	if err != nil {
		fmt.Printf("Failed to dial: %s", err)
	}

}

func (client *SSHClient) test() {
	fmt.Println("test")
}

请注意,这只是一个连接到服务器的示例代码,并没有启用详细输出。要启用详细输出,你需要使用sshConfigConfig字段中的Logger来设置一个自定义的日志记录器。你可以使用ssh包中的NewClient函数来创建一个带有详细输出的SSH客户端。

英文:

i wanted to use golang.org/x/crypto/ssh to write a little program which is able to read the equivalent to ssh -v output. While i am able to connect via the ssh package to my server i have no idea how to enable verbose output or get to the desired information in it. As i am new to golang i am not even able to decide if it is possible at all.

Here is my code so far

package main

import (
	"fmt"

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

type SSHClient struct {
	Config *ssh.ClientConfig
	Host   string
	Port   int
}

func main() {
	sshConfig := &ssh.ClientConfig{
		User: "your_user_name",
		Auth: []ssh.AuthMethod{
			ssh.Password("your_password"),
		},
	}

	client := &SSHClient{
		Config: sshConfig,
		Host:   "example.com",
		Port:   22,
	}

	client.test()
	_, err := ssh.Dial("tcp", "example.com:22", sshConfig)
	if err != nil {
		fmt.Printf("Failed to dial: %s", err)
	}

}

func (client *SSHClient) test() {
	fmt.Println("test")
}

答案1

得分: 1

作为一个快速的解决办法,你可以打开$GOPATH/golang.org/x/crypto/ssh/mux.go文件,将const debugMux = false改为const debugMux = true,然后重新编译你的程序。

为了看到调试日志,请先让你的程序执行一些操作:

s, err := c.NewSession()
if err != nil {
    fmt.Printf("Failed to create session: %s", err)
}
fmt.Println(s.CombinedOutput("hostname"))
英文:

As a quick hack you can open $GOPATH/golang.org/x/crypto/ssh/mux.go file, change const debugMux = false to const debugMux = true and recompile your program.

In order to see the debug logging make your program do something first:

s, err := c.NewSession()
if err != nil {
    fmt.Printf("Failed to create session: %s", err)
}
fmt.Println(s.CombinedOutput("hostname"))

huangapple
  • 本文由 发表于 2016年2月17日 15:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/35450430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定