Solved

using Telnet to get information from a Router using .net

Posted on 2009-07-16
4
707 Views
Last Modified: 2012-05-07
Hello Experts,
         I am trying to get a stream of data from a router. If I use the commands on the command line they work fine. I am trying to get it to work in vb.net with no avail.  In the code pasted below I loop until I get a response for username and password which seems to work.  When I send the commands (system ,shell, top) I will get the response back "Username:telnet" or sometimes "password...".

Thanks in Advance
Bill
Dim myIPAddress As String = "192.168.1.1"
        Dim localAdd As System.Net.IPAddress = IPAddress.Parse(myIPAddress)
        Dim sock As TcpClient
        Dim s As String
        Dim n As Integer
 
        Try
            sock = New TcpClient()
            sock.Connect(myIPAddress, 23)
 
            Dim b As Boolean = sock.Connected
 
            Dim ns As NetworkStream = sock.GetStream
            Dim sr As New StreamReader(ns)
            Dim result As String
 
            Dim sw As New StreamWriter(ns)
           
            Dim b1 As Boolean
 
            Do Until n = 2
                If Not b1 Then
                    sw.WriteLine("telnet")
                    sw.Flush()
                End If
 
                result = sr.ReadLine
 
                If InStr(result, "Username") Then
                    sw.WriteLine("USERNAME")
                    sw.Flush()
                    b1 = True
                    n += 1
                End If
 
                If InStr(result, "Password") Then
                    sw.WriteLine("PASSWORD")
                    sw.Flush()
                    b1 = True
                    n += 1
                End If
 
            Loop
 
 
            sw.WriteLine("system")
            sw.Flush()
            result = sr.ReadLine
 
            sw.WriteLine("shell")
            sw.Flush()
            result = sr.ReadLine
 
            sw.WriteLine("top")
            sw.Flush()
            result = sr.ReadLine
 
            s = result
 
        Catch ex As Exception
            MessageBox.Show("Error " & ex.Message)
        Finally
            sock.Close()
        End Try

Open in new window

0
Comment
Question by:bill_home
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Jan Springer
ID: 24878098
If you don't get an answer to this question, you may want to consider 'rancid' to get the configuration data and changes or write something in perl using the expect module.
0
 

Author Comment

by:bill_home
ID: 24878575
What is rancid?  I noticed that questions of this type are left un-answered all over the web.
0
 
LVL 29

Expert Comment

by:Jan Springer
ID: 24878637
rancid is a tool that is used to monitor a device's configuration.

http://www.shrubbery.net/rancid/
0
 

Accepted Solution

by:
bill_home earned 0 total points
ID: 24897321
I found out how to get around this, it was all about hand-shaking with tcpip I will put the solution here if anyone needs it.. kind of a kludge but it worked

 Dim sReadline As String = ""
        Dim passflag As Boolean
        Dim adminflag As Boolean

        Do
            sw.WriteLine("")
            result = sr.ReadLine
            sReadline = Mid(result, 1, 4)
        Loop Until sReadline = "User" Or sReadline = "Pass"

        Do
            If sReadline = "User" Or adminflag Then
                If ns.CanRead And ns.CanWrite Then
                    passflag = False
                    adminflag = True
                    sw.WriteLine("username")
                    result = sr.ReadLine
                    sReadline = Mid(result, 1, 4)
                End If
            End If


            If sReadline = "Pass" Or passflag Then
                If ns.CanRead And ns.CanWrite Then
                    passflag = True
                    adminflag = False
                    sw.WriteLine("password")
                    result = sr.ReadLine
                    sReadline = Mid(result, 1, 4)
                End If
            End If
        Loop Until sReadline = "Wire"

        Do
            sw.WriteLine("system")
            result = sr.ReadLine
            sReadline = Mid(result, 1, 7)
        Loop Until sReadline = "system>"

        Do
            sw.WriteLine("shell")
            result = sr.ReadLine
            sReadline = Mid(result, 1, 4)
        Loop Until sReadline = "Temp"

        Do
            sw.WriteLine("top")
            result = sr.ReadLine
            sReadline = Mid(result, 1, 80)
            If InStr(sReadline, "Mem", CompareMethod.Binary) > 0 Then
                MessageBox.Show(sReadline) 'mem load
                result = sr.ReadLine
                sReadline = Mid(result, 1, 80) 'blank line
                MessageBox.Show(sReadline)
                result = sr.ReadLine
                sReadline = Mid(result, 1, 80)  'cpu load
                MessageBox.Show(sReadline)
            End If
        Loop ' loop value on form for the amount of times to loop
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Tired of waiting for your show or movie to load?  Are buffering issues a constant problem with your internet connection?  Check this article out to see if these simple adjustments are the solution for you.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question