Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET and Telnet 1

Status
Not open for further replies.

nrt13

Programmer
Apr 3, 2014
3
0
0
US
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:
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
 

.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!
 
I will give that a shot thanks
 
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.

WbHIMo9
 
 http://jmp.sh/WbHIMo9
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top