英文:
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")
}
请注意,这只是一个连接到服务器的示例代码,并没有启用详细输出。要启用详细输出,你需要使用sshConfig
的Config
字段中的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"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论