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