Asked by:

connecting to Telnet through C#

Question

I am trying to create a Telnet session from a C# application and be able to send commands and receive output back. Is this possible with just a regular TCPClient connecting to a host on port 23 and then reading from/writing to the stream associated with that client? This is what I have been trying and can't figure out if my username/password stuff works right, thats why I didn't post any code because i really dont think I have anything that works.

Any help on this would be greatly appreciated.

Thanks in advance

Monday, January 15, 2007 6:59 PM

All replies

You are exactly right. Telnet is one of those ridiculously simply things that for the most part is just an open port... the authentication though is something else completely as that is up to the shell that you are accessing, not so much the telnet protocol itself.

when I just do a Console.Write to what is read into my buffer in the input.Read() call.

On a side note, do you have any idea how to do the authentication? or how to figure out a way to do it? Because since you said that I am guessing that sending in a username/password over the Socket doesnt authenticate me at all and thus I am really not even connected to anything.

I don't know if any of what I said made much sense, sorry if I just confuse you more.

I've been working on getting a telnet session to work from C# using the TCPClient connecting to a Windows Telnet Server. By default the Telnet server in Windows uses NTLM authentication only. You can disable this in the Telnet Server Administration tool (Display/Change registry settings -> NTLM -> set the value to 0 then restart the telnet service). You will then need to create TelnetClients group and add a user account you will used to authenticate with. Using ASCII encoding you connect, read bytes, then write bytes for username and password (make sure to include the Environment.NewLine after each).I can get past the login and password, but then when I try to execute any additional commands my app hangs. I'm expecting a synchronous response, and because I'm using a aspx page I need to keep it synchronous.

Side note:When I first attempted to connect without disabling NTLM I received ??%?????????? from the server, which I am guessing is the authentication challenge - with NTLM disabled that problem went away.

Any ideas on why it would not respond? I tried executing a simple dir command.

The page calls the checkSearch method using a button and it passes the IP and port, and the results are written to a textbox called txtResults.

I spent the rest of the afternoon yesterday working on this and in my research I discovered the problems I was having are related to the Telnet protocol. In Telnet you must implement a Network Virtual Terminal (NVT) and when you open a connection to a telnet server, you must handle the negotiation of things like terminal type. If you need to make this work, http://www.hw-group.com/support/nvt/index_en.html will be a helpful resource. I ended up choosing to skip telnet altogether to tackle my problem - I don't really have the time or patience to write a telnet client right now.

If you do choose to work through getting telnet to work, I recommend logging or putting a watch on the bytes in the byte[] coming from the server every time you do a read. Whenever you see 255 that is telling you that the following byte is to be interpretted as a command (IAC), and it is usally followed by codes for commands or operations (see NVT for more detail). The server will wait for an appropriate response to the commands it sent you and won't respond until it gets them.

If you end up having need for this and get it working please post a follow-up with sample code.

Bit Didler was on right track, and then learned that there's a bit more to a Telnet client than one may thing. However, I believe that it actually could be easier to bypass the Telnet protocol and control all input via a Console session. If
you can redirect standard in and standard out through the Process.Startinfo methods, then you could just use input and output from the dos window! All the OS to netgoiate and run the Telnet session.

JP Cowboy Coders Unite!

Wednesday, April 25, 2012 12:59 PM

Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.