question

RahulFadnavis-7414 avatar image
0 Votes"
RahulFadnavis-7414 asked MotoX80 answered

console output automation


Hello,

Below is my command to perform one activity. It accepts few arguments which is fine and no issues there. But when we run that command in cmd, it asks for password and confirm password.
I want to automate that password and confirm password part. How can I do that?

Example:

When we run below command in cmd -

c:\Test\myscript.exe --arg1 --arg2 --config "c:\Temp\config.cfg" --confirm

It shows below in same command prompt-

Performing the script execution for ABCD.

Enter password:

Type again to confirm:


What I need is, How can I automate this entering of password and confirm password, considering I have the password.
Please help here. Powershell or vbscript solution is preferred.

dotnet-csharpwindows-server-powershelldotnet-visual-basicsmall-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Does the executable have the parameter for passwords?

0 Votes 0 ·

Hi,
Is there any update? Have you got a chance to verify the below suggestions?
Please feel free to let us know if more assistance is needed. If the reply is helpful, please “Accept Answer” to help other community members find it more easily.

0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered

What is myscript.exe? Is it really a script? Powershell? VB script? Or is it a compiled executable?

You can try piping stdin to the program.

 echo MyPassword | myscript.exe 


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RahulFadnavis-7414 avatar image
0 Votes"
RahulFadnavis-7414 answered

Hello,

Consider Myscript.exe as any third party software and we cannot alter or modify it. The exe has fixed arguments which it takes as an input but not the password by default. below powershell link has similar solution but I am not able to make it. Could you please help me here. Please let me know if any further details are needed here. Just check the last script of below link. Something similar I needed.
Hence echo will not work here.

What I actually need is to automate password input for any application/exe via cmd or powershell prompt. something related to New-Object System.Diagnostics.Process should work.

how-to-provide-input-for-prompts-



Please check the very last script of above link. similar I need to implement.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

Here is a Powershell script to use nslookup to query a name. Do you need to look for a specific response? That last script that you pointed out should work.

 $MyProcess = New-Object System.Diagnostics.Process
 $MyProcess.StartInfo.FileName = "nslookup.exe"
 $MyProcess.StartInfo.Arguments = "-"
 $MyProcess.StartInfo.UseShellExecute = $false
 $MyProcess.StartInfo.RedirectStandardInput = $true
 $MyProcess.StartInfo.RedirectStandardOutput = $true 
 $MyProcess.StartInfo.RedirectStandardError = $true 
 $MyProcess.Start()
 $StdIn = $MyProcess.StandardInput
 $StdIn.WriteLine("yahoo.com")
 $StdIn.WriteLine("exit")
 $StdIn.Close()
 $MyProcess.WaitForExit()
 $MyProcess.StandardOutput.ReadToEnd()
 $MyProcess.StandardError.ReadToEnd()
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

I played around some more with calling nslookup and netsh to see if I could simulate a password prompt. I had mixed results. When nslookup outputs a ">", a .readline call will hang on that. I can do a .read to get each character, but once .peek returns a -1, then I have to do a .readline to reset it, otherwise it appears that it will always return a -1 even if more characters are available to be read.

You will just have to test "MyScript.exe" to see how it interacts with console input/ouput.


 Function GetResponse {
     Start-Sleep -Milliseconds 1000                   # give the proram some time to run
     $buffer = $stdout.readline()                     # Assume the we have at least one line of output
     while ($StdOut.peek() -ne -1) {
         $buffer += [char]$StdOut.read()              # read char by char because readline might hang 
     } 
     return $buffer
 }
    
    
    
 $MyProcess = New-Object System.Diagnostics.Process
 $MyProcess.StartInfo.FileName = "nslookup.exe"
 $MyProcess.StartInfo.Arguments = "-"
 $MyProcess.StartInfo.UseShellExecute = $false
 $MyProcess.StartInfo.RedirectStandardInput = $true
 $MyProcess.StartInfo.RedirectStandardOutput = $true 
 $MyProcess.StartInfo.RedirectStandardError = $true 
 $MyProcess.Start()
 $StdIn = $MyProcess.StandardInput
 $StdOut = $MyProcess.StandardOutput
 $StdErr = $MyProcess.StandardError
 cls 
 "---------------Initial prompt-------------"
 GetResponse
 "---------------yahoo-------------"
 $StdIn.WriteLine("yahoo.com")
 GetResponse
 "---------------msn-------------"
 $StdIn.WriteLine("msn.com")               
 GetResponse
 "---------------exit-------------"
 $StdIn.WriteLine("exit")
 GetResponse
    
 while ($MyProcess.HasExited -ne $true) { Start-Sleep -Milliseconds 50 }
 "Final Stdout-------------"
 $StdOut.ReadToEnd()
 "Final Stderr-------------"
 $StdErr.ReadToEnd()
 return 


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RahulFadnavis-7414 avatar image
0 Votes"
RahulFadnavis-7414 answered

Thanks MotoX80 for your response. Unfortunately above script is not working as expected. Above you assume we will enter the password manually but actually we will pass the password as a parameter to powershell script and from there it will read that password and enter the password automatically whenever it prompts for passwords two time. I did not understand the role of GetResponse function in this case.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

GetResponse is just a function that the script calls to get the output of the program. This makes it easier if there are multiple interactions with the program where the script enters data for the program to process and then does something based on the output.

This might be more of what you are looking for but you will need to test it because i do not know what program you are calling and what output it produces.

 param ($password ="")
    
 Function GetResponse {
     Start-Sleep -Milliseconds 1000                   # give the proram some time to run
     $buffer = $stdout.readline()                     # Assume the we have at least one line of output
     while ($StdOut.peek() -ne -1) {
         $buffer += [char]$StdOut.read()              # read char by char because read 
     } 
     return $buffer
 }
    
 if ($password -eq "") {
     "Please enter a password as a command line parameter"
     return
 }
    
 $MyProcess = New-Object System.Diagnostics.Process
 $MyProcess.StartInfo.FileName = "Cscript.exe"                                # program name
 $MyProcess.StartInfo.Arguments = "C:\scripts\myscript.vbs"              # it's command line arguments
 $MyProcess.StartInfo.UseShellExecute = $false
 $MyProcess.StartInfo.RedirectStandardInput = $true
 $MyProcess.StartInfo.RedirectStandardOutput = $true 
 $MyProcess.StartInfo.RedirectStandardError = $true 
 $MyProcess.Start()
 $StdIn = $MyProcess.StandardInput
 $StdOut = $MyProcess.StandardOutput
 $StdErr = $MyProcess.StandardError
 cls 
 "---------------Look for the program to output the work password-------------"
 while ($true) {
     if ($MyProcess.HasExited -eq $true) {
         "The program has ended."
         break
     }
     $buffer = GetResponse
     "Program output-------------------------------------------"
     $buffer
     "End of output buffer-------------------------------------"
     if ($buffer -match 'password') {
         "I found the word password. Entering it now."
         $StdIn.WriteLine($password)
         break
     }
 }
 "Password processing is complete. Waiting until the process ends." 
 while ($MyProcess.HasExited -ne $true) { Start-Sleep -Milliseconds 50 }
 "Final Stdout-------------"
 $StdOut.ReadToEnd()
 "Final Stderr-------------"
 $StdErr.ReadToEnd()
 return 


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.