×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VB.NET and Telnet

VB.NET and Telnet

VB.NET and Telnet

(OP)
I am trying to create an app that will interact with Telnet.
Right now I am using the console app project in VS Studio 2008.

It doesnt seem to work. I want to telnet to a server and send commands.
I don't get any errors in VS, but when I run the project nothing seems to happen. I have a console box pop up but it is just a flsahing cursor.

I would like to accomplish a few things.
1.send commands back and forth via telnet
2.log my telnet session, I thought I could do that with the -f argument that works outside of my project when I use telnet
3.View what is actually happening in telnet. The only way I know to do
this is to set StartInfo.UseShellExecute = True, but if I do that I can see the telnet prompt in that case, but I cannot send standard input etc.
Here is my Code

CODE --> VB.NET

Module Module1

    Public Sub Main()
       

        Dim telnetTest As New Process
        Dim StartInfo As New System.Diagnostics.ProcessStartInfo

    ' Run Telnet
        StartInfo.FileName = "telnet.exe"
        StartInfo.Arguments = "54.204.44.77 -f h:\TestNewGT.txt"
        StartInfo.RedirectStandardInput = True
        StartInfo.RedirectStandardOutput = True

     
        StartInfo.UseShellExecute = False
        StartInfo.CreateNoWindow = True
        telnetTest.StartInfo = StartInfo
        telnetTest.Start()
        Dim SR As System.IO.StreamReader = telnetTest.StandardOutput
        Dim SW As System.IO.StreamWriter = telnetTest.StandardInput

        'Enter Telnet Commands
        SW.WriteLine("My First Command")
        SW.WriteLine("My Second Command")
        SW.WriteLine("my third command")
        Console.ReadLine()
        'Displayes the results...
        SR.ReadToEnd()
        SW.Close()
        SR.Close()

       
    End Sub
End Module 

RE: VB.NET and Telnet


.NET has a built-in telnet client. Use this in your code to access it:

Imports System.Net.Sockets
Imports System.Text


Then something like this:

Dim TelnetClient As TcpClient
Dim ThisStream As NetworkStream
Dim SendBuffer(128) As Byte
Dim ReadBuffer(128) As Byte
Dim ReturnVal As String
Dim ReturnLength As Integer

'Connect to server (next 2 lines)
TelnetClient = New TcpClient("Server Name Or Address Here", 23)

ThisStream = TelnetClient.GetStream


s
'Send Username (next 2 lines)
SendBuffer = Encoding.ASCII.GetBytes("username here")

ThisStream.Write(SendBuffer, 0, SendBuffer.Length)

'Read server's response (next 2 lines)
ReturnLength = ThisStream.Read(ReadBuffer, 0, ReadBuffer.Length)

ReturnVal = Encoding.ASCII.GetString(ReadBuffer)

'Send Password (next 2 lines)
SendBuffer = Encoding.ASCII.GetBytes("password here")

ThisStream.Write(SendBuffer, 0, SendBuffer.Length)


'Read server's response (next 2 lines)
ReturnLength = ThisStream.Read(ReadBuffer, 0, ReadBuffer.Length)

ReturnVal = Encoding.ASCII.GetString(ReadBuffer)

'Display server's response
MsgBox(ReturnVal)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: VB.NET and Telnet

(OP)
I will give that a shot thanks

RE: VB.NET and Telnet

(OP)
Just tried your code and added my ip and username to the code.
Two issues 1.I cannot actually see the telent session execute line by line
2. It doesn't seem to get passed when i enter my username.

The MSGbox just resturns my username, instance and node.
It's doesnt seem like it ever tries to insert the password line.

See attached photo.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close