Jump to content

send a 'Y' to cmd window


 Share

Recommended Posts

Pretty new to AutoIT and would like to ask this question.

can AutoIt monitor a cmd window and reply 'Y' when a certain question is asked?

I have a script that requires a user input either Y or N to carry on with the script, just want AutoIT to see this question and answer with a Y.

I cannot change the script this is running

Many thanks

Link to comment
Share on other sites

Pretty new to AutoIT and would like to ask this question.

can AutoIt monitor a cmd window and reply 'Y' when a certain question is asked?

I have a script that requires a user input either Y or N to carry on with the script, just want AutoIT to see this question and answer with a Y.

I cannot change the script this is running

Many thanks

Yes, there is a way to do this. The best way to do it depends on just what program you are running in the cmd window. What is it called? Then post any code you have written so far so we can fix it.

Some commands have /y switch for confirmations.

Also, there is the StdoutRead function in AutoIt.

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

Many thanks for taking the time to reply.

I am automating an Oracle install that has tree separate scripts to complete such a task. The final script is the only one that requires you to answer Y or N to "are you ready to clone the database" but it is asked through the cmd window not a gui. I have been able to work with the pop up gui during install to answer 'OK' or "next" but I am struggling with this part.

Regards

Link to comment
Share on other sites

So far I have tried:

#include <Constants.au3>

$CMD = Run("cmd.exe", @WindowsDir, @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)

StdinWrite($CMD, "echo HELLO!")
StdinWrite($CMD)

; Read from child's STDOUT and show
ConsoleWrite("+>Debug: " & StdoutRead($CMD) & @CRLF)

To see if I could echo "HELLO!" but all I got was:

Microsoft Windows XP [Version 5.1.2600]

So, I am now trying to see if there is any other way using DLL Calls to read output! That way I can then send it!
Link to comment
Share on other sites

See if you can modify what you find in this post.

Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Link to comment
Share on other sites

Haha I just did this:

$objShell = ObjCreate("WScript.Shell") 
$objExecObject = $objShell.Exec("cmd.exe ipconfig")
$Out = FileOpen("output.txt", 1)

While Not $objExecObject.StdOut.AtEndOfStream
    $strText = $objExecObject.StdOut.ReadLine()
    If StringInStr($strText, "0") > 0 Then
        MsgBox(0, @ScriptName, "Found output.")
        FileWrite($Out, $strText)
        FileClose($Out)
        Exit
    EndIf
WEnd

If any output is found then it notifies you!

Grr, it only outputs the Microsoft information!

Edited by JamesB
Link to comment
Share on other sites

Hey, Group.

James there were a couple of "gotchas" in your test script:

#include <Constants.au3>
           
           $CMD = Run("cmd.exe", @WindowsDir, @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)
           
           StdinWrite($CMD, "echo HELLO!")
           StdinWrite($CMD)
           
          ; Read from child's STDOUT and show
           ConsoleWrite("+>Debug: " & StdoutRead($CMD) & @CRLF)
The first is that StdinWrite doesn't make any assumptions about your input; it only sends what you put in and nothing more. CMD.EXE expects a CR/LF to signal the end of any command line, so you'd need to include that at the end of the string you're sending.

The other is that you need to wait for an indication from CMD.EXE that it's output everything that it's going to. StdoutRead (and its relatives) return any output that's immediately available, but you need to take measures to insure that the child process doesn't have more output that you may be interested in. I've seen coders use ProcessWaitClose() to test that the child process has exited, but there's the chance that the child write can so much data that space used to hold it is used up and the child process will halt (because it can't finish writing its output) and never exit.

For this reason most scripts that use StdoutRead will have a section like:

While 1
    $ourOutput &= StdoutRead($CMD)
    If @error Then ExitLoop
WEnd

StdoutRead sets @error whenever the child process writes EOF to its output (or any other time that it judges that there will never be any more output from the child). We have to use @error as a test because we can't draw any conclusions from the return of an empty string; it may be that the child process didn't write any output since we last tested...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

There's also another way to automatically send an answer to an input(and it's actually a built in feature in dos):

Here comes an example:(probably a bad one, as I can't think of any other dos command that accepts an input right now).

Say we want to run the dos set command, with the /p parameter(this waits untill the user inputs something and presses enter) :

set /p abc=

To demonstrate how one could automate such an input situation, you can do this:

Opt("WinTitleMatchMode", 4)

$abc = "Some funky string, yay!"

FileDelete(@ScriptDir & "\toot.tmp")
FileWrite(@ScriptDir & "\toot.tmp", $abc)

Run(@ComSpec & " /k set /p abc=<toot.tmp", @ScriptDir)

WinWait("[CLASS:ConsoleWindowClass]")
WinActivate("[LAST]")
Sleep(250)
ControlSend("[LAST]", "", "", "echo %abc%{enter}")

The only thing that actually matters is how the dos command is run:

set /p abc=<toot.tmp

This will make it take the content of the file toot.tmp and enter it into the Set's input field.

The example in AutoIt code became a lil big, but that's for demonstrational purposes. :)

Edit:

Also, if you want to save the output of a a dos command, you can use the > director instead(< means in, > means out)

for example:

echo Hohoh, the leetest string evarr! > myfile.txt
;or
ping www.google.com >myping.txt

Notice that if you do not specify a directory proceeding your filename(like mydir\myfile.txt) then it will output it in the current directory the cmd window is in(and there's a fairly large chance that this might be your %userprofile% folder, ie. in my case, C:\Documents and Settings\FreeFry)

Edited by FreeFry
Link to comment
Share on other sites

Ahh cool! I learnt something new :)

Nice to hear. :)

While 1
    WinActivate("[CLASS:ConsoleWindowClass]")
    If Not WinWaitActive("[CLASS:ConsoleWindowClass]", "", 1) Then ContinueLoop
    Send("!{space}{down 6}{right}{down 3}{enter 2}") ; copy text to clipboard
    If StringInStr(ClipGet(), "are you ready to clone the database") Then ExitLoop
    Sleep(1000)
WEnd
ControlSend("[CLASS:ConsoleWindowClass]", "", "", "Y{Enter}")
I'm not very fond about using Send as it's not 100% reliable, neither is ControlSend I'm afraid. But I guess, whatever rows your boat. :)
Link to comment
Share on other sites

No luck I'm afraid at the moment, here is what I have done...

#include <Constants.au3>

$PID = Run("C:\temp\scripts\Patchset10gR2\swinstall\Patchset10gR2.cmd", @SystemDir, $STDIN_CHILD + $STDOUT_CHILD)

While 1

$line = StdoutRead($PID)

If @error Then ExitLoop

If StringInStr($line, "Do you want to proceed? [y|n]") Then

StdinWrite($PID, "y" & @CRLF)

StdinWrite($PID)

ExitLoop

EndIf

Wend

but I do not get an input of "Y"

Please shutdown Oracle instances running out of this ORACLE_HOME on the local system.

(Oracle Home = 'e:\oracle\product\10.2.0\AGILdb')

Is the local system ready for patching?

Do you want to proceed? [y|n]

it then just sits there ......

Also where does one put the Constants.au3 file???

Regards

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...