dousong3760 2014-01-14 23:24
浏览 60
已采纳

使用Go SSH库与Cisco设备通讯

I'm trying to implement monitoring various Cisco ASR parameters doing ssh to router and using show commands to get results. Here is part of the workflow (error handling omitted):

client, err := ssh.Dial("tcp", "172.16.32.95:22", config)
session, err := client.NewSession()
sshOut, err := session.StdoutPipe()
sshIn, err := session.StdinPipe()
err := session.RequestPty("xterm", 80, 40, modes)
err := session.Shell()

At this point I can write to sshIn and read from sshOut. Since Cisco's (and other vendor's) routers don't support exec'ing commands (correct me if I'm wrong) all I can do is to pass commands to shell and read input until I find command prompt.

Ok. Here is part I use to skip initial router greetings before the first command prompt:

buf := make([]byte, 1000)
n, err := sshOut.Read(buf) //this reads the ssh terminal welcome message
loadStr := ""
if err == nil {
    loadStr = string(buf[:n])
}
for (err == nil) && (!strings.Contains(loadStr, "[local]")) {
    n, err = sshOut.Read(buf)
    loadStr += string(buf[:n])
}
fmt.Println(loadStr)

which yields:

Last login: Tue Jan 14 17:29:06 -0500 2014 on pts/1 from 172.16.35.101.
Cisco Systems SSI
[local]ewag# 

To run command I write to sshIn:

if _, err := sshIn.Write([]byte("show clock")); err != nil {
    panic("Failed to run: " + err.Error())
}

And read response the same way I read initial greeting, which yields (with greetings):

Last login: Tue Jan 14 18:06:14 -0500 2014 on pts/2 from 10.7.7.14.
Cisco Systems SSI
[local]ewag# 
show clock
Tuesday January 14 18:09:19 EST 2014
[local]ewag# 

Still everything ok. But when I try to send somewhat longer command, f.e.:

if _, err := sshIn.Write([]byte("show session progress ipsg-service ipsg-gprs-svc")); err != nil {
    panic("Failed to run: " + err.Error())
}

Output breaks:

Last login: Tue Jan 14 18:09:19 -0500 2014 on pts/2 from 10.7.7.14.
Cisco Systems SSI
[local]ewag# 
show session progress ipsg
-service ipsg-gprs-svc
Unknown command - "ipsg-service", unrecognized keyword
[local]ewag# 

The command I've sent got split at some point, but output for command result is passed correctly without splits.

So question is how this split can be fixed? For now I'm out of ideas. Thanks for attention!

  • 写回答

2条回答 默认 最新

  • douhe8981 2014-01-15 11:05
    关注

    Stupid mistake. I was requesting pty this way:

    session.RequestPty("vt100", 80, 40, modes)
    

    which gives me width 40 chars, and height 80 chars. In my case the correct call should be:

    session.RequestPty("vt100", 0, 200, modes)
    

    This way everything works as expected. And there is no need to change terminal width explicitly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?