473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

interacting with an interactive cli program

Hi

I'm trying to write a program that interacts with an interactive cli
program, that is a program that does some processing but every now and then
requires some user input. My problem is that when I redirect stdin and
stdout of the program I launch, the program seems to run but I never get any
output from it. But if I don't redirect its stdout, I see how it's just
sitting there waiting for my input. To illustrate the problem and make
testing easier I wrote two very small programs, one simulating my
interactive program:

static void Main(string[] args)
{
Console.Write("Login[]");
Console.ReadLine();
Console.Write("\r\nPassword[]:");
Console.ReadLine();
Console.Write("\r\nDo you wish to continue? y/n");
string answer = Console.ReadLine();
if (answer.Equals("y"))
Console.WriteLine("yippie");
else
Console.WriteLine("aborting");
}

And the one supposed to handle this cli program:

static void main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo("c:\\temp\\inputreader.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start(psi);
StreamReader reader = proc.StandardOutput;
string line = "";
while ((line = reader.ReadLine())!=null)
{
if (line.Equals("Login[]"))
{
proc.StandardInput.WriteLine("login");
}
if (line.Equals("Password[]:"))
{
proc.StandardInput.WriteLine("password");
}
if (line.Equals("Do you wish to continue? y/n"))
{
proc.StandardInput.WriteLine("y");
}
}
}

If anybody could tell me where I'm going wrong I'd much appreciate it.

Regards
Stephan
Nov 16 '05 #1
5 4243
Stephan Steiner <st*****@isuisse.com> wrote:
I'm trying to write a program that interacts with an interactive cli
program, that is a program that does some processing but every now and then
requires some user input. My problem is that when I redirect stdin and
stdout of the program I launch, the program seems to run but I never get any
output from it. But if I don't redirect its stdout, I see how it's just
sitting there waiting for my input. To illustrate the problem and make
testing easier I wrote two very small programs, one simulating my
interactive program:


<snip>

The problem is that you're using Console.Write instead of
Console.WriteLine - so you're never finishing the current line, so
reader.ReadLine() can never return.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
>
The problem is that you're using Console.Write instead of
Console.WriteLine - so you're never finishing the current line, so
reader.ReadLine() can never return.


Ahh, now I get it. Thanks.

This solves the problems with my test program, but I'm still not quite done
with the one I really want to launch (the Cisco VPN client). Basically I
know it writes entire lines to stdout, but if it requires manual input, any
proc.StandardOutput.Read/ReadLine/Peek blocks indefinitely. Even a C++
sample I found that redirects cmd.exe into a window blocks when I launch the
VPNClient so I know it's not the language and the mechanism I'm catching
stdin and stdout also seems to be okay so I'm wondering what more it could
be.

Do you have any idea what could be the reason that even if my stdin/stdout
writer/reader program waits until I know for sure that the vpnclient I have
launched is waiting for an input (all I have to do is not redirect stdout to
see), a simple proc.StandardOutput.Peek blocks indefinitely?

Regards
Stephan
Nov 16 '05 #3
Stephan Steiner <st*****@isuisse.com> wrote:
The problem is that you're using Console.Write instead of
Console.WriteLine - so you're never finishing the current line, so
reader.ReadLine() can never return.


Ahh, now I get it. Thanks.

This solves the problems with my test program, but I'm still not quite done
with the one I really want to launch (the Cisco VPN client). Basically I
know it writes entire lines to stdout, but if it requires manual input, any
proc.StandardOutput.Read/ReadLine/Peek blocks indefinitely. Even a C++
sample I found that redirects cmd.exe into a window blocks when I launch the
VPNClient so I know it's not the language and the mechanism I'm catching
stdin and stdout also seems to be okay so I'm wondering what more it could
be.

Do you have any idea what could be the reason that even if my stdin/stdout
writer/reader program waits until I know for sure that the vpnclient I have
launched is waiting for an input (all I have to do is not redirect stdout to
see), a simple proc.StandardOutput.Peek blocks indefinitely?


Hmm... not sure, to be honest. It may well be doing some strange stuff
with the console to (for instance) make passwords get echoed as ****
etc. If that's the case, you probably won't have much luck - but maybe
someone else here knows more about it...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
>
Hmm... not sure, to be honest. It may well be doing some strange stuff
with the console to (for instance) make passwords get echoed as ****
etc. If that's the case, you probably won't have much luck - but maybe
someone else here knows more about it...


I tend to agree with your assessment. I've just come across a resolved bug
when reading the cisco vpn client release notes.. it was about piping
stdin/stdout not working properly. Maybe they've reintroduced that bug in
the latest version that I'm using. I have now contacted Cisco about this as
I'm not able to find the error on my end. I was not even able to just
redirect stdin and look at how it plays out seeing stdout on screen.

Regards
Stephan
Nov 16 '05 #5
>
Hmm... not sure, to be honest. It may well be doing some strange stuff
with the console to (for instance) make passwords get echoed as ****
etc. If that's the case, you probably won't have much luck - but maybe
someone else here knows more about it...


I've managed to get it working - the Cisco VPN Client was at fault and I had
to downgrade to an older version that does not have any issues with
redirecting stdin/stdout. But I've come across another thing that I find
quite strange. I realized that I could send all my input to stdin right
away, then just let her rip so to speak and read every line from the stdout
until the process exists (well, it's a bit more complex as the process is
not ended unless you start another instance of the vpnclient and tell it to
disconnect, which also leads to the termination of the first vpnclient).
Anyway, the issue I've come across is this:

Since at first I thought I had to read characters from stdout (which is
correct if I want to enter my input at the appropriate position.. the login
prompt is not an entire line, the curser remains on the same line so
proc.StandardOutput.readLine would block indefinitely), but using
proc.StandardOutput.Peek to check if there's any more data on the stdout is
unreliable. If rather than stepping through the program and read character
by character I stop once peek returns -1, I set a breakpoint after my while
loop that reads from stdout, the while loop is ended prematurely. And while
peek returns -1, a proc.StandardOutput.readLine still returns valid lines.
Is there a peeping Tom protection in Peek? What could cause Peek to
return -1 when ReadLine returns a valid line?

And in code in case my explanation was a bit complicated:

char[] blub = new char[1];
int peakval = 0;
StreamReader reader = proc.StandardOutput;
while ((peakval = reader.Peek()) != -1)
{
proc.StandardOutput.Read(blub, 0, 1);
Console.Write(blub, 0, 1);
}

does not yield the same result as

StreamReader reader = proc.StandardOutput;
string line = "";
while ((line = reader.ReadLine())!=null)
{
Console.WriteLine(line);
}

The latter gets all the lines, the former does not. But putting the two
after each other, the ReadLine based while gets the remaning lines from the
stdout. The inverse (forcefully aborting the ReadLine based while loop is
required though), does not work.
Regards
Stephan
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Reed | last post by:
I seem to remeber reading somewhere there was a statement you could put in your python program to stop its execution and start the interactive interpreter at that point (and then you could change...
6
by: Avi Berkovich | last post by:
Hello, I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for starts, it doesn't show the version information at the beginning and won't return anything when writing to the...
20
by: Joe | last post by:
When you run "python -i scriptname.py" after the script completes you left at the interactive command prompt. Is there a way to have this occur from a running program? In other words can I...
3
by: DGG | last post by:
I have got a question arsing from my perl program. Basically, I want to get the machine date and time, and display it on a Tk label. I am using ActivePerl, and running on DOS/Windows. So I...
0
by: Diego Zeballos Fraigne | last post by:
Hi there, we have a problem when using NetDDE from a non-interactive session Our solution is a VB6 program which - among other things - requests data from a DDE server. The program is not a...
0
by: flat_ross | last post by:
I have a service running under SYSTEM in XP that needs to notify the interactive user. From all of my newsgroup reading it looks like it is no longer acceptable to set the service as "Interact...
2
by: Allan Adler | last post by:
In C, I can execute system("gs gleep.ps"); and have C run a ghostscript program gleep.ps, which might ask for user input and, when it gets it, take some actions and report back to the C...
10
by: notejam | last post by:
I am trying to get started with a interactive version of Python for windows and need some help. I have played with the tutorial, and now want to write a program. In basic language, I could write...
3
by: R. Bernstein | last post by:
The next release of pydb will have the ability to go into ipython from inside the debugger. Sort of like how in ruby-debug you can go into irb :-) For ipython, this can be done pretty simply;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.