Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2008, 11:33 AM
Registered User
 
Join Date: Dec 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default intergrating telnet through System.Diagnostics.Pro

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 ...

 
Old March 11th, 2008, 08:39 AM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rrossenbg Send a message via Yahoo to rrossenbg
Default

Hi sudhirbharti,

Why don't you try this.


Process myProcess = Process.Start(myProcessStartInfo);
string myString = myProcess.StandardOutput.ReadToEnd();

Hope it helps

 
Old March 11th, 2008, 08:56 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Because unfortunately that doesn't work....

The Telnet application does not appear to take input and output from the standard input and output.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 11th, 2008, 10:54 AM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rrossenbg Send a message via Yahoo to rrossenbg
Default

You can also read this:

http://www.willasrari.com/blog/run-a...et/000155.aspx

You may find what you miss.

Nothing is impossible Sam.

 
Old March 11th, 2008, 11:19 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The principle is sound, its just the telnet program does not play by the rules. This is likely to be a security feature as allowing access to passwords/etc via the standard input/output streams might be a security risk.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
System.Diagnostics.Process.Exited Event bmains General .NET 1 February 25th, 2008 07:23 AM
System.Diagnostics in C# 2005 Express onion2k C# 2005 1 January 28th, 2008 12:42 PM
System.diagnostics sapsatin General .NET 0 January 11th, 2007 09:26 AM
System.Diagnostics.Process doesn't work on W2K M/C kan_lenny Pro VB.NET 2002/2003 1 December 23rd, 2006 03:53 AM
System.Diagnostics.Process doesn't work on W2K m/c kan_lenny VB.NET 0 October 2nd, 2006 07:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.