我正在尝试在cisco开关上通过ssh运行命令并将结果打印到控制台。 我已成功使用debian linux服务器完成此操作。然而,在cisco开关上,命令的执行会阻止主函数,它永远不会完成。
package main
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
)
func main(){
// Client configuration
config := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{
ssh.Password("mypassword"),
},
}
//Connection
fmt.Println("Connecting...")
client, err := ssh.Dial("tcp", "10.0.0.1:22", config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
fmt.Println("Connected...")
//Session
fmt.Println("Creating Session...")
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
fmt.Println("Session created...")
defer session.Close()
// Execute a single command
var b bytes.Buffer
session.Stdout = &b
fmt.Println("Running command...")
if err := session.Run("show environment all"); err != nil {
panic("Failed to run: " + err.Error())
}
fmt.Println("Command run: Output:", b.String())
}
返回(在session.Run()处挂起:
Connecting...
Connected...
Creating Session...
Session created...
Running command...
此刻我正在黑暗中钓鱼。任何帮助表示赞赏! 谢谢!
修改 我知道我的程序完全挂在这里:
if err := <-s.errors; err != nil && copyError == nil {
copyError = err
}
在Go的x / crypto / ssh包(session.go)的Wait()函数中。但我不确定为什么。好像在这一点上我们正在等待会话错误通道中的错误。但在这一点上,我的技能让我感到不安......
编辑2: 当我运行我的代码时,我的CISCO交换机会在日志中告诉我以下内容。
SWU-ACS-02#26-May-2015 19:07:50 %AAA-I-CONNECT: User CLI session for user admin over ssh , source 10.0.0.2 destination 10.0.0.1 ACCEPTED, aggregated (1)
26-May-2015 19:08:09 %AAA-I-CONNECT: User CLI session for user admin over ssh , source 10.0.0.2 destination 10.0.0.1 ACCEPTED
编辑3: 这是具有最详细选项(-vvv)
的ssh的输出admin@10.0.0.1's password:
debug3: packet_send2: adding 48 (len 61 padlen 19 extra_pad 64)
debug2: we sent a password packet, wait for reply
debug1: Authentication succeeded (password).
Authenticated to 10.0.0.1 ([10.0.0.1]:22).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug2: fd 3 setting TCP_NODELAY
debug3: packet_set_tos: set IP_TOS 0x08
debug1: Sending environment.
debug3: Ignored env TERM
debug3: Ignored env SHELL
debug3: Ignored env XDG_SESSION_COOKIE
debug3: Ignored env SSH_CLIENT
debug3: Ignored env SSH_TTY
debug3: Ignored env USER
debug3: Ignored env LS_COLORS
debug3: Ignored env MAIL
debug3: Ignored env PATH
debug3: Ignored env PWD
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: Ignored env SHLVL
debug3: Ignored env HOME
debug3: Ignored env LANGUAGE
debug3: Ignored env LS_OPTIONS
debug3: Ignored env LOGNAME
debug3: Ignored env SSH_CONNECTION
debug3: Ignored env _
debug1: Sending command: show environment all
debug2: channel 0: request exec confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 256 rmax 128
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0