douchuang1861 2016-01-08 13:49
浏览 50

使用Go的crypto / ssh在Cisco Switch上运行命令

I'm trying to run commands over ssh on a cisco switch and print the result to the console. I have successfully done this with a debian linux server. On the cisco switch however the execution of the command blocks the main function and it never finishes.

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())
}

Return (hangs at session.Run() ) :

Connecting...
Connected...
Creating Session...
Session created...
Running command...

I am fishing in the dark at the moment. Any help is appreciated! Thank you!

Edit: I know that my program hangs exactly here:

if err := <-s.errors; err != nil && copyError == nil {
        copyError = err
}

within the Wait() function of Go's x/crypto/ssh package (session.go). I am however not sure why. Seems like at this point we are waiting to get an error from the sessions errors-channel. But at this point my go skills leave me hanging...

Edit 2: When I run my code, my CISCO switch tells me the following in the log.

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

Edit 3: This is the output of ssh with the most verbose option (-vvv)

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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办