0

I would like to connect telnet with either PowerShell or cmd.

System.Diagnostics.ProcessStartInfo procStartInfo =
            new System.Diagnostics.ProcessStartInfo("cmd.exe", "/k " + "telnet 192.168.1.23 23");

        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();

        //proc.StandardInput.WriteLine("\x3");
        //Console.WriteLine(proc.StandardOutput.ReadToEnd());

        proc.StandardInput.Write("\x01");
        proc.StandardInput.Write("200");
        proc.StandardInput.Close();

        proc.WaitForExit();
        Console.ReadLine();

After connect telnet I would like to send input CTRL + a And 200 number. After I need to wait till I get output, mostly I will get in less than a second but not instant.

I tried with multiple stuff and googling, but no luck each time with each small change of code getting different errors.


I tried with socket programming but don't think I am getting any output from here.

System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        clientSocket.Connect("192.168.1.23", 23);
        Console.WriteLine("Connecting");

        NetworkStream serverStream = clientSocket.GetStream();
        byte[] outStream1 = System.Text.Encoding.ASCII.GetBytes("\x01");
        serverStream.Write(outStream1, 0, outStream1.Length);
        byte[] outStream2 = System.Text.Encoding.ASCII.GetBytes("200\r\n");
        serverStream.Write(outStream2, 0, outStream2.Length);
        serverStream.Flush();

        Console.WriteLine("Write");

        byte[] inStream = new byte[1000768];
        serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
        //serverStream.Read(inStream, 0, 1000768);
        string returndata = System.Text.Encoding.ASCII.GetString(inStream);

        Console.WriteLine(returndata);

Here is the python code that is working perfectly fine for me.

port    = 23
timeout = 1

tn = telnetlib.Telnet('192.168.1.23',port,timeout)
#tn.set_debuglevel(8)
tn.write(b'\x01')                               # Ctrl + A (Send Header)
tn.write(b"200\n")                              # Write 200 (To Get Output)
time.sleep(2)                                   # Sleep 2 sec to read out put
result = tn.read_until(b'\x03')
result = result.decode("utf-8")
tn.close()
AjmeraInfo
  • 506
  • 3
  • 10
  • 25
  • Why the detour with `cmd` (or `PowerShell`)? Why `telnet`? I guess it would be much easier to just create a socket yourself and handle it. And it would be more portable, `telnet` isn't installed by default on Windows anymore (let alone other system, which also lack `cmd` and `PowerShell`). – sticky bit Dec 27 '20 at 23:59
  • @stickybit I do have to read telnet only. And I do have full server access so I have installed telnet. – AjmeraInfo Dec 28 '20 at 01:10
  • Yes OK, so you can install `telnet` and don't care about other systems, got that. But still it seems like an unnecessary detour for me. Creating and handling a socket seems much, much simpler to me as this IPC mess that has no apparent advantages (OK sockets are IPC too, but you know what I mean). – sticky bit Dec 28 '20 at 01:15
  • @stickybit Can you please provide some example code? I have to read every morning one time with 6 different telnet server and read gas fuel gallons. It's veeder root machine and I am reading telnet with an NIC card. Let me know if that helps. – AjmeraInfo Dec 28 '20 at 01:38
  • For starters: ["https://learn.microsoft.com/en-us/dotnet/framework/network-programming/socket-code-examples"](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/socket-code-examples) – sticky bit Dec 28 '20 at 01:41
  • @stickybit I tried with Socket but did not get any output. I have added my socket code into question. Let me know if anything wrong or missing. – AjmeraInfo Dec 28 '20 at 01:59
  • In your other code you have `"\x01"` not `"\x3"`, could that be wrong? And maybe you have to send a terminating `"\r\n"`? – sticky bit Dec 28 '20 at 07:22
  • Yes, I have updated that one. But still can't read with readstream. I added a working python code. If you can help me with it. – AjmeraInfo Dec 28 '20 at 11:44
  • 1
    For example https://www.nuget.org/packages/Telnet/ ? – Caius Jard Dec 28 '20 at 20:01
  • But is it really telnet or is it some other purely socket thing and it just happens that you're using Windows telnet to read it? – Caius Jard Dec 28 '20 at 20:02
  • @CaiusJard It's normal telnet. And I have to read only one time once a day to read gas tank's gallons. – AjmeraInfo Dec 28 '20 at 20:07
  • @CaiusJard Thank you. It worked with that lib. I am sure even in my code I am doing something silly mistake. – AjmeraInfo Dec 28 '20 at 20:49
  • I guess it's truly a telnet server and doing s bit of protocol nego at the start then. You should post an answer saying what lib you used and what the code looks like now – Caius Jard Dec 28 '20 at 20:51

1 Answers1

0

Finally, it worked with Telnet lib. https://www.nuget.org/packages/Telnet/

using var client = new Client("192.168.1.23", 23, new System.Threading.CancellationToken());

await client.Write("\x01");
await client.Write("200");

Thread.Sleep(2000); // My Telnet responde in 2 seconds.

var data = await client.ReadAsync();
AjmeraInfo
  • 506
  • 3
  • 10
  • 25