I need to automate an command line application. It asks the user to enter a password. All my approches to send the password via STDIN failed. Now I am trying to do this with an wrapper-programm using .NET.
I am starting the application creating a new process, setting the StartInfo
-properties and then start the process:
Dim app_path As String
Dim app_args As String
Dim myProcess As Process = New Process()
myProcess.StartInfo.FileName = app_path
myProcess.StartInfo.Arguments = app_args
myProcess.StartInfo.UseShellExecute = False
myProcess.Start()
I did try to use the StartInfo.RedirectStandardInput
Property but with no success.
Now I came accross the WriteConsoleInput
function from the kernel32.dll
that I included like this:
Declare Function WriteConsoleInput Lib "kernel32.dll" Alias "WriteConsoleInputA" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Boolean
I can get the handle of the process via the myProcess.Handle
property. But sending input to the input-buffer using this way was also not possible.
I found those questions but they did not help:
How do I write ‘PAGE DOWN’ into the console input buffer? (1475353)
Java - passing input into external C/C++ application (1421273)
Controlling a Windows Console App w/ stdin pipe (723424)
Using StraceNtX.exe I got this output for the moment the app is waiting for input:
[T4024] GetConsoleMode(f, 12d35c, 12d3af, 77bff894, ...) = 1
[T4024] SetConsoleMode(f, 0, 12d3af, 77bff894, ...) = 1
[T4024] ReadConsoleInputA(f, 12d348, 1, 12d360, ...) = 1
Can anyone tell me, what else to try or how to do the above the right way? Thanks!
Based on Tim Robinsons answere I've got this code now, but it does not work:
myProcess = New Process()
myProcess.StartInfo.FileName = app_path
myProcess.StartInfo.Arguments = app_args
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
myProcess.StartInfo.UseShellExecute = False
myProcess.Start()
' Wait for process requesting passwort input
System.Threading.Thread.Sleep(3000)
Dim len As Integer
len = 0
Dim handle As Integer
handle = GetStdHandle(STD_INPUT_HANDLE)
WriteConsoleInput(handle, "Test", 4, len)
My programm is an commandline application that should act as an wrapper.
The input is send but in a way that it is not typed into the password field but that below the password field a new promt is shown (without even showing the input).
Tim, can you give me an example?
WriteConsoleInput
expects a console handle, not a process handle. Your problem is how to get hold of this console handle.If your process is a console app, and you don't redirect anything when starting the child process, then the child process will be attached to your own process's console. In which case, you may be able to:
GetStdHandle(STD_INPUT_HANDLE)
to get hold of your own console handleWriteConsoleInput
If you have a GUI app you can assign yourself a console using
AllocConsole
.Edit: I didn't notice at first, but that's not the right definition for
WriteConsoleInput
. It takes an array ofINPUT_RECORD
, not a string. My own experiment with runas.exe works OK once I copied the function prototype from pinvoke.net.Sorry for my late reply, been very busy.
Here is an full code example, stripped down from my code. I hope I did not delete something crucial.
It uses the following module:
With this function you can start the process and wait for a line of output (i.e. "password:"). Then the function will enter the provided text followed by an return.
I hope this helps!
Regards sc911