VB.NET TELNET

Has anyone come across a TELNET class for VB.NET?  I have my FTP portion working but I need to execute a script on a Unix machine and the only way I know how to do this is through a TELNET prompt.  I am basically sending 3 files through FTP, on of which is a Script that i need to run.
anitahelpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anitahelpAuthor Commented:
That looks great but I do not want to register a .dll, trying to get away from this.  Is there source code for the .dll that I can put into a class?
zbowlingCommented:
You can always just open port 23 on a socket and go right in. Here is some code in C#. http://www.csharphelp.com/archives/archive239.html

There is the prefect script for this in the MS .NET samples for this. Its the VB.NET Telnet Client code. Check it out.
http://www.microsoft.com/downloads/details.aspx?FamilyId=08E3D5F8-033D-420B-A3B1-3074505C03F3&displaylang=en

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

anitahelpAuthor Commented:
Ok, looked through all that.  How do I log in?  Change directories?  Read a response from the port?  Thanks for your help, this may be easier than I am thinking, just a little confused!
zbowlingCommented:
this might help. this was converted from my C# app.


Sub Main()
        Dim host As String = "192.168.1.50"
        Dim TCPClient As New TcpClient

        'Connect to telnet
        TCPClient.Connect(host, 23)

        'Get our networkstream
        Dim netstream As NetworkStream = TCPClient.GetStream()
        Dim buffer(1024) As Byte

        'Get the ASCII encoder to convert the text back and forth on the stream (bytes <--> strings)
        Dim encoding As New System.Text.ASCIIEncoding
        Dim hasLoggedUser, hasLoggedPass As Boolean



        Do While True
            'Begin reading data into buffer (this statement is blocking)
            netstream.Read(buffer, 0, buffer.Length)

            'Clean up for displaying
            Dim bufferedstring As String = encoding.GetString(buffer)
            bufferedstring = Replace(bufferedstring, vbCr, "")
            bufferedstring = Replace(bufferedstring, vbLf, "")
            bufferedstring = Replace(bufferedstring, Chr(0).ToString(), "")
            Console.WriteLine(" RECV:  {0}", bufferedstring)

            If hasLoggedUser = False And InStr(encoding.GetString(buffer), "login:") Then
            'Send User
                Dim sendBuffer() As Byte = encoding.GetBytes("my user" & vbCr)
                netstream.Write(sendBuffer, 0, sendBuffer.Length)
                Console.WriteLine("SENT: {0}", encoding.GetString(sendBuffer))
                hasLoggedUser = True

            ElseIf hasLoggedPass = False And InStr(encoding.GetString(buffer), "Password:") Then
            'Send Pass
                Dim sendBuffer() As Byte = encoding.GetBytes("my password" & vbCr)
                netstream.Write(sendBuffer, 0, sendBuffer.Length)
                Console.WriteLine(" SENT: {0}", encoding.GetString(sendBuffer))
                hasLoggedPass = True

            ElseIf hasLoggedPass And hasLoggedUser Then
            'here is where you can get the prompt and do your cd and whatever. you can do it all at once but i don't recommend it. You sould wait for a response.
                Console.WriteLine("All done...")
                Exit Do
            End If

        Loop
        Console.Read()

    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anitahelpAuthor Commented:
Thanks you guys!  I didn't get it figuredout but I am working it.  On the last post I am still confused by the TcpClient portion, do I need to register a control?  Anyway, thanks for your time!
zbowlingCommented:
Nope.. its just in the System.Net and the System.Net.Sockets namespace... just refrences those at the top of or code, just like how System is done for you. Etheir that or you can change every call to TcpClient to System.Net.TcpClient...
jr3Commented:
This is the best code I could find on the internet to create a telnet session using tcpclient. The line:"Console.WriteLine(" RECV:  {0}", bufferedstring) " should return an ascii string but it I Received:  ??:??e??e??¼??‘. I am expecting to receive "username:" Can you check if this code returns the prompt you are expecting and if not can you send a piece of code that works. All I need is something that converts the received network stream into a legible string. I am thinking that should be pretty simple.

I have been working on this for 2 days.

Please help if you can
Thanks


Imports System.Net.Sockets
Module Module1
    Sub Main()
        Dim host As String = "10.10.69.17"
        Dim TCPClient As New TcpClient
        'Connect to telnet
        TCPClient.Connect(host, 23)
        'Get our networkstream
        Dim netstream As NetworkStream = TCPClient.GetStream()
        Dim buffer(1024) As Byte
        'Get the ASCII encoder to convert the text back and forth on the stream (bytes <--> strings)
        Dim encoding As New System.Text.ASCIIEncoding
        Dim hasLoggedUser, hasLoggedPass As Boolean
        Do While True
            'Begin reading data into buffer (this statement is blocking)
            netstream.Read(buffer, 0, buffer.Length)
            'Clean up for displaying
            Dim bufferedstring As String = encoding.GetString(buffer)
            bufferedstring = Replace(bufferedstring, vbCr, "")
            bufferedstring = Replace(bufferedstring, vbLf, "")
            bufferedstring = Replace(bufferedstring, Chr(0).ToString(), "")
            Console.WriteLine(" RECV:  {0}", bufferedstring)          
            If hasLoggedUser = False And InStr(encoding.GetString(buffer), "username:" Then
                'Send User
                Dim sendBuffer() As Byte = encoding.GetBytes("My User" & vbCr)
                netstream.Write(sendBuffer, 0, sendBuffer.Length)
                Console.WriteLine("SENT: {0}", encoding.GetString(sendBuffer))
                hasLoggedUser = True

            ElseIf hasLoggedPass = False And InStr(encoding.GetString(buffer), "password:") Then
                'Send Pass
                Dim sendBuffer() As Byte = encoding.GetBytes("My Pass" & vbCr)
                netstream.Write(sendBuffer, 0, sendBuffer.Length)
                Console.WriteLine(" SENT: {0}", encoding.GetString(sendBuffer))
                hasLoggedPass = True

            ElseIf hasLoggedPass And hasLoggedUser Then
                'here is where you can get the prompt and do your cd and whatever. you can do it all at once but i don't recommend it. You sould wait for a response.
                Console.WriteLine("All done...")
                Exit Do
            End If

        Loop
        Console.Read()

    End Sub

End Module
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.