This project is read-only.

LOVE this project! I have a bizarre issue I'd like your opinion on...

Nov 7, 2013 at 2:17 AM
VB.NET 2012
Win7/64
Project: Interact on command line with Cisco router
How using: Compiled (no modifications) Renci.SshNet to DLL

Firstly, even without me getting this working 100%, it is already the obvious choice for my use.

[My] Problem: I can't figure out a reliable way to send & receive data as if I'm logged in via SSH to a Cisco router.

My issue: I'm finding precious few examples (I write in C# & VB) of doing this, so I've been at it for many hours trying this function and that... I'm not sure I'm doing this correctly at all. Somehow, there is an extra carriage return passed into my SSH session, and although (as you can see) eventually it logs on with enable, unless I can figure out how to send commands and receive full output in return (without extra CRs), I can't write a solid Function() to do it all the time.

The following code, and output, describe the strangeness I'm seeing better than words:

[WARNING: THIS IS UGLY TEST CODE!]
Private Sub RunTest()
            Dim r As String = ""
            Try
                client.Connect() 
                ' created via: Me.client = New Renci.SshNet.SshClient(config.Host, config.User, config.Pass)
                If client.IsConnected Then
                    Debug.WriteLine("connected? " & client.IsConnected)
                    ss = GetShellStream(client)
                End If
                r = ss.Expect(New Regex(">\s*"), System.TimeSpan.FromSeconds(3))
                Debug.WriteLine("r1: [" & r & "]")
                '========================================
                ss.WriteLine("enable")
                r = ss.Expect(New Regex("Password: "), System.TimeSpan.FromSeconds(3))
                Debug.WriteLine("r2: [" & r & "]")
                '========================================
                ss.WriteLine("theEnablePassword")
                r = ss.Expect(New Regex("#"), System.TimeSpan.FromSeconds(3))
                Debug.WriteLine("r3: [" & r & "]")
                '========================================
                 client.Disconnect()
            Catch ex As Exception
                Debug.WriteLine("exception: " & ex.ToString())
            End Try
            Debug.WriteLine("Bye!")
            Environment.Exit(0)
        End Sub
The output is where the issue makes itself apparent:
connected? True
r1: [Type help or '?' for a list of available commands.

COL-FRMGT-A> ]
r2: [enable
Password: ]
r3: [
Invalid password     <<<<<<<<<<<<< Where did this CR/LF come from?
Password: *****************

COL-FRMGT-A# ]
Bye!
GetShellStream takes the SshClient, and resturns SshStream:
Private Function GetShellStream(C As Renci.SshNet.SshClient) As Renci.SshNet.ShellStream
            Dim ss As Renci.SshNet.ShellStream = Nothing
            Try
              ss = C.CreateShellStream("xterm", 80, 24, 800, 600, 1024)
           Catch ex As Exception
                LogIt(TraceEventType.Critical, "GetShellStream() cannot create new ShellStream: " & ex.ToString())
          End Try
            Return ss
        End Function
Are there any examples of how to do this? There is no shortage of subs & functions, but precious few examples of the proper way to interact with the command line like this. Or at least Google can't find them!

Thanks again for the efforts of the developers and user community. Maybe I can help somehow...

TIA!

pat
:)
Nov 7, 2013 at 2:23 AM
Could this be the problem?

below is from: https://sshnet.codeplex.com/workitem/1584

And if it is, is there a simple way to just select which is used via a switch/option? I'm sure there are cases for each way...
Remove Line Feed from WriteLine method in Shellstream Class

description

When working with the expect method, it was discovered the WriteLine was sending an linefeed after a carriage return. This was throwing some SSH implementations off because the linefeed is being interpreted as a second carriage return.
 
I'm suggesting that:
 
var commandText = string.Format("{0}{1}", line, "\r\n");
 
be replaced with
 
var commandText = string.Format("{0}{1}", line, "\r");
Marked as answer by BogusException on 11/8/2013 at 6:48 PM