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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?