launching a commandline program from a c# app

S

Stephan Steiner

Hi

I'm trying to automate VPN connection establishment and disconnection using
the Cisco VPN client. That particular client has an interactive cli
interface. I set my program up to launch the VPN client, redirect its
standard input and output, and write to its standard input where a manual
user intervention is required. However, I've come across a problem:
Basically, if I launch the program in a configuration that doesn't require
any manual user interaction, things work just fine and I can read the output
from the redirected stdout until the vpn client exits. However, if user
input is required, when I try to read from the stdout, my program just sits
there doing nothing. But in the background, the vpn client is waiting for
data to be entered (I've tried not redirecting stdout and sure enough the
vpn client is doing its thing, then just sits there waiting for input).

Here's a snippet of my code:

string arguments = "connect " + vpnprofilename + " cliauth";
ProcessStartInfo psi = new ProcessStartInfo(vpnExe, arguments);
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start(psi); // start the vpnclient
StreamReader reader = proc.StandardOutput;
while ((line = reader.ReadLine())!=null)
{
if (line.IndexOf("Username [") != -1)
{
proc.StandardInput.WriteLine(this.login);
}
if (line.IndexOf("Password [") != -1)
{
proc.StandardInput.WriteLine(this.password);
}
}

If I have no connection error, the vpnclient should eventually write
"Username ["... to the stdout, which I try to catch and upon which I'd then
write the proper password to the stdin. However, I never get to this point.
I never get beyond the while line unless that program runs and exits without
any user intervention (in which case things work as they should but there's
no way to set up a connection fully automatic). Does anybody have any idea
as to how I can capture the stdout of a launched 3rd part cli program if
that one eventually requires some input from stdin (but not right away) and
won't exit until it has received such input?

Regards
Stephan
 
F

Francois Beaussier

Hi

string arguments = "connect " + vpnprofilename + " cliauth";
ProcessStartInfo psi = new ProcessStartInfo(vpnExe, arguments);
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start(psi); // start the vpnclient
StreamReader reader = proc.StandardOutput;
while ((line = reader.ReadLine())!=null)

Are you sure that the server send a end of line character ? ReadLine
will wait until that happens. If the server only send "Password: " for
example, the call will wait forever !

Use the Read() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top