System.Diagnostics.Process for telnet.exe

  • Thread starter Thread starter Horst Walter
  • Start date Start date
H

Horst Walter

What I try to accomplish is to run "telnet.exe" as a process in C#.

The C#-code below works with terminating commands, e.g. a
"HelloWorld.exe".

Since I'd like to communicate with "telnet" the process is still
running when I already have to read from the stream. This seems to be
the problem, since "myStreamReader.ReadLine()" is waiting for
something.

Is there a chance to force the underlying pipe to flush its output (at
least the password line should be there), so that I can read
immediately.

Any Ideas?
Regards HW

--------
Code:

namespace Telnet
{
class Telnet
{
static void Main(string[] args)
{
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new
ProcessStartInfo("telnet", "192.168.0.1");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardInput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
StreamWriter myStreamWriter = myProcess.StandardInput;

// problem, here it hangs
// Read() and ReadToEnd() also hangs!
string myString = myStreamReader.ReadLine(); <=PROBLEM ?
....
....
....


Purpose of the application:

What I want to do is call a remote application, read the telnet
stream, search for a string (e.g. password) and respond (e.g. by
sending he "pw")

The reason why I do not want to use a library is that I have to extend
the programm later using ssh, rsh ...
 
Thanks for the info. Unfortunately the Site is down.

However, my idea still is to get it going with standard (.exe)
programs, because in my particular case this would be more generic:

Otherwise I'd need a lib for telnet, one for ssh, one for rsh...

Best Regards
HW


Nicholas Paldino said:
Horst,

Instead of channeling the input/output through the command window, why
not use a programattic component for Telnet? Josh Mitts has developed one,
and you can find it at (watch for line wrap):

http://www.treasureonthenet.com/net/v2/projects.asp

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Horst Walter said:
What I try to accomplish is to run "telnet.exe" as a process in C#.

The C#-code below works with terminating commands, e.g. a
"HelloWorld.exe".

Since I'd like to communicate with "telnet" the process is still
running when I already have to read from the stream. This seems to be
the problem, since "myStreamReader.ReadLine()" is waiting for
something.

Is there a chance to force the underlying pipe to flush its output (at
least the password line should be there), so that I can read
immediately.

Any Ideas?
Regards HW

--------
Code:

namespace Telnet
{
class Telnet
{
static void Main(string[] args)
{
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new
ProcessStartInfo("telnet", "192.168.0.1");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardInput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
StreamWriter myStreamWriter = myProcess.StandardInput;

// problem, here it hangs
// Read() and ReadToEnd() also hangs!
string myString = myStreamReader.ReadLine(); <=PROBLEM ?
...
...
...


Purpose of the application:

What I want to do is call a remote application, read the telnet
stream, search for a string (e.g. password) and respond (e.g. by
sending he "pw")

The reason why I do not want to use a library is that I have to extend
the programm later using ssh, rsh ...
 
Back
Top