0

I want to ask you how to execute the Telnet command through cmd using c#. I am able to do (ping,tracert) but unable to do Telnet.

The objective of my app is to check the network connectivity of the servers. So i am trying to ping,telnet and tracert the server ip. But i am stuck with telenet .. The error cmd is showing "'telnet' is not recognized as an internal or external command, operable program or batch file." I have enabled the telnet client from add and remove program steps.

My code for executing telnet is

System.Diagnostics.ProcessStartInfo procStartInfo =
       new System.Diagnostics.ProcessStartInfo("cmd.exe", "/k " + telnet 192.168.74.74 21;

procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = false;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;

proc.Start();
proc.WaitForExit();

Appreciate all the comments kindly please help.. Waiting for the solution.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Madcoder
  • 23
  • 1
  • 7
  • See http://stackoverflow.com/questions/390188/c-sharp-telnet-library – Alex K. Dec 15 '14 at 15:15
  • 1
    Why not call `telnet.exe` directly? There is also a syntax error for the code you pasted into your question. The `procStartInfo` line will not compile. – grovesNL Dec 15 '14 at 15:16
  • My major objective is to check the status of server, – Madcoder Dec 15 '14 at 15:24
  • we execute telnet ip port no and chek the status of the server and take a snapshot of cmd and sent them, so thats why i am executing it threw process – Madcoder Dec 15 '14 at 15:25

1 Answers1

4

I think Process.Start is the wrong way to do this.

You can use the Ping class to do a ping, and the Socket class to do some communication with the Telnet server (maybe something like here). In that way you don't have to use Process.Start and go through all the output reading and parsing problems.

Concerning your actual problem: I think it is related to 32-64 bit stuff. If your program runs in 32 bit mode on a 64 bit machine, the software can't find telnet. If you uncheck the Prefer 32 bit in the Project Properties > Build, it finds the file and the program works.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • actually i want to write in cmd and check the hops whether its is failing or connecting to the required ip, and then take a snapshot of cmd. – Madcoder Dec 15 '14 at 15:26
  • 1
    @PatrickHofman OP says " I have enabled the telnet client from add and remove program steps." Also there is no confirmation that OP can run telnet from CMD directly... – Alexei Levenkov Dec 15 '14 at 15:34
  • 1
    Patrick Hofman-Thank You so much, you have given the exact solution the problem. Cheers keep on helping Sir.!!! – Madcoder Dec 15 '14 at 15:54
  • @PatrickHofman Do you know to enable such setting from the command line? – Lime Sep 02 '15 at 04:31
  • What setting @william? – Patrick Hofman Sep 02 '15 at 07:01
  • @PatrickHofman `Project Properties > Build` I'm trying to do something similar here http://stackoverflow.com/questions/32341057/windows-automate-telnet?noredirect=1#comment52557392_32341057 – Lime Sep 02 '15 at 07:07
  • What about using [`TcpClient`](https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx)? See the sample included there. – Patrick Hofman Sep 02 '15 at 07:16