Question

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\r")); 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\r")); 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!

Was it helpful?

Solution

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.

OTHER TIPS

Problem was resolved by setting terminal width on Cisco. So, after session is established, I send command:

terminal width 200

to router.

Now long commands are displayed correctly in replies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top