Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: How to maintain a 'conversation' with an SSH session?

$
0
0
@egagne, I see now that PowerShell is very similar. I, too, took notes from the same emails, and came up with a more .NET-ish solution. So it wouldn't be taken offline, I posted the whole thing (sample code, output, etc.) on my blog here.

To preserve space here, I will say that I created for my tests several functions, a few of which are:
Function SendCommand(cmd As String, s As ShellStream) As String
        Try
            reader = New StreamReader(s)
            writer = New StreamWriter(s)
            writer.AutoFlush = True
            writer.WriteLine(cmd)
            While s.Length = 0
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            Debug.WriteLine("SendCommand(" & cmd & ") caught exception: " & ex.ToString())
        End Try
        Return reader.ReadToEnd()
    End Function
    Function SendCommandW(cmd As String, s As ShellStream) As String
        Try
            reader = New StreamReader(s)
            writer = New StreamWriter(s)
            writer.AutoFlush = True
            writer.Write(cmd)
            While s.Length = 0
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            Debug.WriteLine("SendCommandW(" & cmd & ") caught exception: " & ex.ToString())
        End Try
        Return reader.ReadToEnd()
    End Function
On the blog I take the time to explain why there is more than one SendCommand(). You can probably tell from the above why-especially if you have had to work with Cisco IOS for any length of time.

So all you so is create a ShellStream, and pass it to one of the functions. That function will return only the text from the command. This was the hard part. Once I got it to work, it seemed nothing I tried would clear out the buffers-making all previous input/output part of the current output!

I write in lots of languages, but since everyone here discusses in C# (and the source is in C#), I thought I'd help out the VB.NET folks.

This project, and the DLL/library it creates, are really great. Since I don't know what kind of development help is needed, right now all I can do is pass on some working code for others to get over the difficulties that plagued me for about 20 hours!

Thanks again, folks, and let me know if there is any way I can contribute.

pat
:)

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>