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条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题