writing a Telnet client with VB. NET
Hi, I am trying to write a simple telnet client using VB.NET with Visual Studio 2005.
I am using System.Net.Sockets.
But I cant make it work yet.
What I need is:
To automate some tasks on UNIX server.
To achieve this, I need to login to unix (from within my app.) via telnet, and run some commands.
Thats all.Anyone can help me with this ?
Thanks
Fede.
Answers
Hey,
Download this sample from
Its just 1 source file that will compile to an assemply and you can use its public methods to script your telnet session. I had the same issue where I had to script a telnet session for a cisco interface using telnet. You may have to register to download the sample.
If your not able to figure it out, let me know and I can post some sample code.
All Replies
- Is there a specific problem you are having with the socket communication. If you can narrow down your question we will better be able to answer it.
yes,
I'am Using
System.Net.Sockets
tcpClientBy now i succeeded to connect to the unix server.
I recieve this as a response: "??↑??▼??#??'??".
I tried to parse it with Encoding.Ascii without success. I dont get anything.
How to negotiate the authentication process ?
Thanks
Fede
Hey,
Download this sample from
Its just 1 source file that will compile to an assemply and you can use its public methods to script your telnet session. I had the same issue where I had to script a telnet session for a cisco interface using telnet. You may have to register to download the sample.
If your not able to figure it out, let me know and I can post some sample code.
- Hi there,I've the smae issue and I've downloaded the ScriptTelnet C# project from the URL provided above.Now how do I implement using that class in VB.NET?I apperciate any help in advance.
Ok. The first thing you should do is make sure the downloaded project compiles ok. Then create a new project, I would do just a Console app for testing, add a reference to the .dll file and then do something like the following:
Imports
objActiveSolutionsModule
Module1
Sub Main()
Dim ip As String = "66.xx.xx.xx" Dim port As Integer = 23 Dim timeout As Integer = 10'create the object supplying the parameters
Dim session As New ScriptingTelnet(ip, port, timeout) Dim connected As Boolean = session.Connect() 'try to connect If connected = True Then
Dim startingPrompt As Integer = session.WaitFor("Username:") If startingPrompt = 0 Then'connected sucessfully
"testuser", "Password:")'the username prompt was found so we send the username
'and wait to receive our password prompt
session.SendAndWait(
'now send the password and wait for our next prompt
'in this sample I expect to receive a success message
session.SendAndWait(
"testpassword", "Success") Else
"Username prompt was not found") End If ElseConsole.WriteLine(
"Unable to connect to the server") End If End SubConsole.WriteLine(
End
ModuleI hope you get the feel of how to use this component to script your session.
- hey TDAVISJR thaks so much for the example above.I compiled and used the DLL as you suggested, but I'm getting an exception. It makes a successful connection to the server. However it waits for the "login:" prompt, and the time out occure while waiting. I tried with longer timeout values and that didn't resolve it. Do you know what may be causing that exception?Thanks for all the help.Wekkaw
- Well, other than making sure your looking for the correct prompt because it will time out if it don't find the prompt. Also, you could try stepping into the code and see where it fails. I'm not really an expert on the inner workings of this component. I used it and it worked. I don't think there is any contact info for the author of the code so you can shoot him an email, but you can double check that. I don't know how much more I can help. Just do your best with it, if it doesn't work out then its back to the drawing board.
- TDavisjr, thans so much for the help. I've tried stepping in but I'm not seeing anything yet.I'll keep on working on it tho. Thanks again for putting me to the right direction.Wekkaw
- I im trying to write a Telnet client for pocket Pc...can you help me?
I am having the same issue as well. I copied and pasted the prompt for "Username: " from the telnet window that I have, but it still does not work.
Anyone have any ideas?
Thanks,
Eric
Guys,
I don't know what is going on here. I was in the same situation you were all in until I found that component. I gave it a shot and it just worked. I have no clue why it worked for me and not for you all. Now, there are other components out there can can script telnet sessions; but of course they are not free. Good luck you all.
This code does not work for me either. It does not seem to be sending messages properly.
- I found something in http://www.codeproject.com/cs/internet/terminalcontrol.asp , look at the file TerminalControl.cs (negociating sequence commands)
Hello,
This is a very good control.
But how can I simulate "Enter" command into terminal ?
Thanks
- TDAVIS or anyone that has this working in C#
could you either post or send me some source code of how you used the code you found? I am trying to do it but it isn't working for me for some reason.
Also did you just add the ScriptingTelnet class to your project or did you do it some other way? If some other way could I get information on how to do that as well.
Thanks
my email is fradam97 -at- gmail -dot- com if you would rather not post stuff. el_Fede wrote: yes,
I'am Using
System.Net.Sockets
tcpClientBy now i succeeded to connect to the unix server.
I recieve this as a response: "??↑??▼??#??'??".
I tried to parse it with Encoding.Ascii without success. I dont get anything.
How to negotiate the authentication process ?
Thanks
Fede
To anyone having this problem and wanting to know why, I figured out that it is the Telnet server trying to negotiate with you. If you run through the debugger (or print out) the actual byte numbers that correspond to those garbage symbols then look up negotiating a telnet connection online you will be able to figure it out.
Here is the site I found that helped me.
http://www.scit.wlv.ac.uk/~jphb/comms/telnet.html- the code was very useful for me and it worked. i wanted to convert this console application to a windows application. That is, if the username and password in the code matches, then i should get a form with two text boxes (- one for entering the command input (like show version) and another textbox for receiving output) and a command button. where should i change the code? in dll file or the console app code?
add VbCrLf
as in: Me.textbox1.text & vbcrlf
- I have exactly same problem and any VB.NET example there?
- Real briefly, connect a TCP socket (stream) and read the "login:" prompt, send the userid, read the "Password:" prompt, send the password, then wait for the shell "#" prompt and send the command.
Telnet sends some basic header information which you can safely ignore. You just have to read a string off the stream and look for "login:", etc.
Also remember that UNIX systems (if that is what you are connecting to) uses "\n" (vbLf for VB guys) not "\r\n" (vbCrLf) - Hi everyone,
I've got some problems with telneting a device too.
I can connect to a device authenticate with username and password, but can't execute any command after that.
After sending some commands the receive buffer content hadn't changed. I suppose that I the commands are not send to the device.
The device I try to connect is MSAN - Huawei UA5000.
Can enyone help me?
Here is the code I use:
Sub
Main() Dim session As New System.Net.Sockets.TcpClient Dim networkstream As Net.Sockets.NetworkStream
Dim bytes(session.ReceiveBufferSize) As Byte Dim ne_data As String Dim send_command1("username".Length) As Byte Dim send_command2("password".Length) As Byte Dim send_command3("enable".Length) As Byte Dim send_command4("config".Length) As Byte Dim send_command5("test".Length) As Byte Dim send_command6("pots loop-line-test telno 074984046".Length + 2) As Byte Dim i As Integersession.Connect(
"10.240.73.132", 23) If session.Connected ThenDebug.Print(
"connected") If session.GetStream.CanRead Thennetworkstream = session.GetStream
Debug.Print(
"Sending user name")send_command1 = System.Text.Encoding.ASCII.GetBytes(
"username" & vbCrLf) 'with vbCrLf successfuly can sand username and passwordnetworkstream.Write(send_command1, 0, send_command1.Length)
networkstream.Read(bytes, 0,
CInt(session.ReceiveBufferSize))ne_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Debug.Print(
"Device said:" & ne_data & vbCrLf)Debug.Print(
"Sending password")send_command2 = System.Text.Encoding.ASCII.GetBytes(
"msan" & vbCrLf)networkstream.Write(send_command2, 0, send_command2.Length)
Threading.Thread.Sleep(1000)
' wait to receive datanetworkstream.Read(bytes, 0,
CInt(session.ReceiveBufferSize))ne_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Debug.Print(
"Device said:" & ne_data & vbCrLf)Debug.Print(
"Sending ENABLE") 'cannot send command to the devicesend_command3 = System.Text.Encoding.ASCII.GetBytes(
"enable" & vbLf) '??? here is the problem ???networkstream.Write(send_command3, 0, send_command3.Length)
Threading.Thread.Sleep(1000)
' wait to receive datane_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Debug.Print(
"Device said:" & ne_data & vbCrLf)'some other code
networkstream.Close() ElseDebug.Print(
"can't read") End If ElseDebug.Print(
"connection failed") End Ifsession.Close()
If session.Connected ThenDebug.Print(
"still connected") ElseDebug.Print(
"disconnected") End If End Sub - I am using this example and when it connect I get '??%' back so the wait for 'password:' times out.
Any ideas? - telent is pretty complex, certainly more than just opening and writing to a port. i wrote an application to automate a bunch of tasks using telnet.
i used w3Sockets from Dimac. it worked great.
DimacWebSite
- is there any documentation for the dimac w3socket.dll?
- Broken link?


