Jump to content

Unable to Execute a Simple DOS Command Using AutoIT


Recommended Posts

I want to be able to execute a command in DOS-Prompt (CMD) from Windows 7 but I could not figure out how.  I've already read the Macros File, Run, and a bunch of other threads which brought me to think AutoIT simply doesn't work with DOS command.  Maybe Powershell will work?  I know it's a blasphemy but I'm hoping someone can prove me wrong.

Here's my code:

Run(@ComSpec & " /c  C:\cpqlocfg.exe -s 10.11.22.33 -f dl360g7-01.xml", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

The ".xml" file is supposed to change password on the server.

The script would run with no errors but no changes have been made when I logged into the server.  The old password still works.  One thing to note is that cpqlocfg.exe can only be executed through command in DOS.  If I just run the code below, I'll just get a DOS splash screen.

Run (@Scriptdir, & "\cpqlocfg.exe")

When I actually run the command "cpqlocfg.exe -s <IP Address> -f <filename.xml> in DOS prompt, this is the output that I'm supposed to get:

C:\>cpqlocfg.exe -s 10.11.22.33 -f dl36
0g7-01.xml

HP Lights-Out Configuration Utility- CPQLOCFG v. 4.01 dated 08/28/2012
 (c) Hewlett-Packard Company, 2012

Server is 10.11.22.33

 CHECKING XML SYNTAX...
<s> START_OPEN_TAG
<s> RIBCL
<s> VERSION
<s> EQUALS
<s>  2.0
<s> END_TAG
<s> START_OPEN_TAG
<s> LOGIN
<s> USER_LOGIN
<s> EQUALS
<s>  Administrator
<s> PASSWORD
<s> EQUALS
<s>  testpassword
<s> END_TAG
<s> START_OPEN_TAG
<s> USER_INFO
<s> MODE
<s> EQUALS
<s>  write
<s> END_TAG
<s> START_OPEN_TAG
<s> MOD_USER
<s> USER_LOGIN
<s> EQUALS
<s>  Administrator
<s> END_TAG
<s> START_OPEN_TAG
<s> PASSWORD
<s> VALUE
<s> EQUALS
<s>  password99
<s> CLOSE_OPEN_TAG
<s> START_CLOSE_TAG
<s> MOD_USER
<s> END_TAG
<s> START_CLOSE_TAG
<s> USER_INFO
<s> END_TAG
<s> START_CLOSE_TAG
<s> LOGIN
<s> END_TAG
<s> START_CLOSE_TAG
<s> RIBCL
<s> END_TAG


Valid XML Syntax

Negotiated cipher: 168-bit Triple DES with RSA and a SHA1 MAC

<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
<INFORM>Scripting utility should be updated to the latest version.</INFORM>
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>
<?xml version="1.0"?>
<RIBCL VERSION="2.22">
<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
     />
</RIBCL>

cpqlocfg.exe: Script succeeded on "10.11.22.33:443"

C:\>

This is all that I'm getting from executing the script in AutoIT:

>"C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Users\username\Documents\AutoIT\test5.au3" /UserParams    
+>11:17:50 Starting AutoIt3Wrapper v.2.1.2.9    Environment(Language:0409  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64)
>Running AU3Check (1.54.22.0)  from:C:\Program Files (x86)\AutoIt3
+>11:17:50 AU3Check ended.rc:0
>Running:(3.3.8.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\username\Documents\AutoIT\test5.au3"    
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
+>11:17:52 AutoIt3.exe ended.rc:0
>Exit code: 0    Time: 2.522
Edited by dreamzboy
Link to comment
Share on other sites

First, if you wanted the output from the program, why are you redirecting the StdOut?

Second, have you tried running the program from AutoIt without hiding everything to see of there's an error? You are hiding the console window with @SW_HIDE, and then you're immediately closing the console window with the /c parameter, yet you haven't gotten it working so you have no idea if you have an error condition that needs to be fixed or not.

Have you tried running it with the WorkingDir parameter set to where the xlm file is, or using the full path and file name of the XML file, or both?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thanks for the quick reply.

Here's what was suggested but that didn't work so I searched on this forum and used someone else's code tailored to mine but that didn't work.

To run DOS (console) commands, try Run(@ComSpec & " /c " & 'commandName', "", @SW_HIDE) ; don't forget " " before "/c"

Trying your suggestion I get cricket chirping.  :)  Nothing shows up and no errors.  By the way, the IPs are mismatched from this post to my original post but I can assure you that I have the right IP address.  You'll just have to trust me.

Run(@ScriptDir & "\cpqlocfg.exe -s 10.112.186.122 -f lab-dl360g7-01.xml", "", @SW_SHOW)
Edited by dreamzboy
Link to comment
Share on other sites

  • Developers

 Try:

Run(@ComSpec & " /c C:\cpqlocfg.exe -s 10.112.186.122 -f lab-dl360g7-01.xml", "c:\", @SW_SHOW)

 

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

 Try:

Run(@ComSpec & " /c C:\cpqlocfg.exe -s 10.112.186.122 -f lab-dl360g7-01.xml", "c:\", @SW_SHOW)

 

Jos

 

You are God!  This works but is there a way to keep the DOS prompt open so I can see whether it executed successfully?  If I remove the " /c " that BrewMan mentioned to keep the window open, the script stopped at the current directory.

Edited by dreamzboy
Link to comment
Share on other sites

You are God!  This works but is there a way to keep the DOS prompt open so I can see whether it executed successfully?  If I remove the " /c " that BrewMan mentioned to keep the window open, the script stopped at the current directory.

 

use /k instead of /c to keep dos window alive:

Run(@ComSpec & " /k C:\cpqlocfg.exe -s 10.112.186.122 -f lab-dl360g7-01.xml", "c:\", @SW_SHOW)

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

You guys rocks!  :thumbsup:  Sorry to have doubted AutoIT.  :bonk:

 

I will never doubt AutoIT again.

I will never doubt AutoIT again.

I will never doubt AutoIT again.

I will never doubt AutoIT again.

 

One last question:  Can you actually detect the line below in the command prompt?  It's to confirm that it actually did succeed as a part of my error detection measure.  Sorry for having so many questions.  I don't see too many example on this subject.  I'm willing to learn it if you can show me where.  Thanks again!

cpqlocfg.exe: Script succeeded on "10.11.22.33:443"

Link to comment
Share on other sites

if you do not use i/o redirection, you can't detect the output of the dos command, but you only see it (with eyes) it in the dos windows,
to interact with the dos window try something like this:

#include <Constants.au3>
Local $Output

; here we start a hidden DOS prompt to be used when needed (it remains alive becouse of /K parameter)
; the @SW_HIDE is used because when there is redirection, the dos window shows nothing (so there is not neet to showw it)
Global $prompt = Run(@ComSpec & " /K", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD + $STDERR_MERGED)
Do ; wait untill prompt is ready
    Sleep(100)
Until StringInStr(StdoutRead($prompt), ">")

; launch of your program
StdinWrite($prompt, "C:\cpqlocfg.exe -s 10.112.186.122 -f lab-dl360g7-01.xml" & @CRLF)

Do ; this should output to the consol the program's output
    $Output = StdoutRead($prompt)
    ConsoleWrite($Output)
    If StringInStr($Output, 'succeeded on "10.11.22.33:443"') Then
        ; ConsoleWrite(@CRLF & "--- Finished ---" & @CRLF)
        MsgBox(0, "", 'DETECTED end string -> succeeded on "10.11.22.33:443" <- in dos output', 5)
        ExitLoop
    EndIf
Until 1 > 1
ProcessClose($prompt)
Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Thank you again.  I'll give that a try.

This is where I have the confusion but hopefully your example can shed some light when I execute it.

$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD + $STDERR_MERGED

You also mentioned the " /k " will keep the dos screen alive but the @SW_HIDE will hide it? :huh2:

Edited by dreamzboy
Link to comment
Share on other sites

Thank you again.  I'll give that a try.

This is where I have the confusion but hopefully your example can shed some light when I execute it.

$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD + $STDERR_MERGED

You also mentioned the " /k " will keep the dos screen alive but the @SW_HIDE will hide it? :huh2:

 

$STDIN_CHILD: enables CMD.EXE to receive commands via StdinWrite() instead that from the keyboard;

$STDOUT_CHILD; enables CMD.EXE to send the output to "your script" instead that to the DOS black screen, you read it with StdoutRead() into a variable

$STDERR_CHILD: same as $STDOUT_CHILD but for error messages

$STDERR_MERGED: error messages are mixed to stdout_child so that you read them together with dos output with StdoutRead()

when you use /K the CMD.exe will keep alive,  but if you also use I/O redirection at the same time, then is better to hide the dos window with @sw_hide, becouse with I/O redirection the cmd.exe will work in "background" and nothing will be visible on the black dos screen also if you use @sw_show to see the black dos window on the screen, instead you will comunicate with it only via I/O streams with StdinWrite() to send commands and StdoutRead() to read the output.

(p.s. to see various possible dos parameters open a dos prompt and type cmd /? inside it)

I post this example trying to show some different behaviours of cmd.exe depending of parameters /k or /c or I/O redirection

#include <Constants.au3>

; - - - - - - - - - - - - - - - - - -
$cmd_Pid = Run(@ComSpec & " /c dir c:\")

; this run cmd.exe pass it the "dir c:\" command and then cmd.exe will close itself (and so disappear from screen)
; is useful when you have to run a program and there is not need to follow what happens nor interact with it

MsgBox(0, "/c example", 'the command dir "c:\" was run with this command: Run(@ComSpec & " /c dir c:\")' & @CRLF & _
                        'the command was executed and the output displayed in the DOS window,' & @CRLF & _
                        'but it is closed immediately at the end of execution because of the /c parameter')

; - - - - - - - - - - - - - - - - - -
$cmd_Pid = Run(@ComSpec & " /k dir c:\")

; this run cmd.exe and pass it the "dir c:\" command, then cmd.exe will stay on screen
; but you can not interact with it via I/O strams. You can only interact with it via keyboard and see output on screen)

MsgBox(0, "/k example", 'now the command "dir c:\" was run  with this command: Run(@ComSpec & " /k dir c:\")' & @CRLF & _
                        'and the dos windows remains open because of the /k parameter' & @CRLF & _
                        "but you can not interact with it via I/O streams." & @CRLF & @CRLF & "click OK to close dos windows and go on")
ProcessClose($cmd_Pid)

; - - - - - - - - - - - - - - - - - -
$cmd_Pid = Run(@ComSpec & " /c dir c:\", "", @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)

; this run cmd.exe, pass to it the "dir c:\" command and redirect I/O strams to your script
; in this way to "get" the otput you must read it from the output stream with the StdoutRead() command until @error occurs
; you will knows that the command has finished by testing the @error macro. It works in this way:
; when the command finished, the cmd.exe closes itself (because of /c) and so when you try to read more output stream @error is set

Local $s_Buf
While Not @error
    $s_Buf &= StdoutRead($cmd_Pid) ; get output from dos command via output stream
WEnd

MsgBox(0, "/c and I/O redirection example", 'now the command "dir c:\" was run with this command: Run(@ComSpec & " /c dir c:\", "", @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)' & @CRLF & _
                                            'and the dos output was read via StdoutRead()' & @CRLF & _
                                            'in fact it is not sent to dos windows but to output stream via I/O redirection' & @CRLF & _
                                            'also in this way at the end of the command execution dos windows closes because of /c' & @CRLF & _
                                            'here is the output of that command, read by the StdoutRead()' & @CRLF&@CRLF& $s_Buf)

; - - - - - - - - - - - - - - - - - -
$cmd_Pid = Run(@ComSpec & " /k echo off & prompt $Q$B$C$L$A$G$F$B$Q", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) ; prompt is =|(<&>)|=

; this only run cmd.exe and pass I/O strams to you (now you MUST manage streams by yourself)
; echo off & prompt $Q$B$C$L$A$G$F$B$Q are used to set a "special" prompt to be used to detect end of command execution
; in this way the cmd.exe runs in background and remains alive because of the /k parameter (untill your script ends)
; you can use it via I/O streams by sending dos commands with StdinWrite() and then read the output with StdoutRead()
; the problem in this way is to know when a command has finished (because no @error is set at the end of the command execution)
; so you can use this way if you have to execute commands and then interact with output
; by waiting and detect some "special" strings and react conseguently

MsgBox(0,"/k an I/O redirextion example",'The cmd.exe has been spawn by the Run(@ComSpec & " /k", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD) command' & @CRLF & _
                                       'it is now running in background and we can "use" it via I/O streams' & @CRLF & _
                                       'to send commands we use StdinWrite()' & @CRLF & _
                                       'to read output we use StdoutRread()' & @CRLF & _
                                       'then to know if the command has finished, we can check, for exeample,' & @CRLF & _
                                       'the presence of the "prompt" in the output. this indicates that the execution of a command is terminated' & @CRLF & _
                                       'to simplify the detection of the prompt, we use a "modified" prompt so that it is easily recognizable.' & @CRLF & _
                                       'for this purpose the prompt is set to this string "=|(<&>)|=" by the dos command "prompt $Q$B$C$L$A$G$F$B$Q"' & @CRLF & @CRLF & _
                                       'now, when you click OK, we send the "ping 127.0.0.1 -n 10" command to cmd.exe')

 StdinWrite($cmd_Pid, "ping 127.0.0.1 -n 10 & echo on" & @CRLF) ; echo on will show the special string =|(<&>)|= after the end of the command

MsgBox(0,"/k an I/O redirextion example",'the "ping 127.0.0.1 -n 10" command has been sent to cmd.exe via StdinWrite()' & @CRLF & _
                                       'to "see" the output of that commands we use StdoutRead()' & @CRLF & _
                                       'and to know if the command has finished, we can check' & @CRLF & _
                                       'if the "=|(<&>)|=" string is present in the output' & @CRLF & _
                                       'now, when you click OK, we read what is present on the output stream with the StdoutRead()' & @CRLF & _
                                       'and we also check for the presence of the "=|(<&>)|=" string')

Local $finished = False
do
$s_Output = StdoutRead($cmd_Pid) ; we read output of cmd.exe

if StringInStr($s_Output,"=|(<&>)|=") Then ; custom dos prompt detected in output

    MsgBox(0,"StdoutRead()",'this is the output from the cmd.exe:' & @CRLF & @CRLF & _
                             $s_Output & @CRLF & @CRLF & _
                            'because the "=|(<&>)|=" string is present' & @CRLF & _
                            'we know that the command has finished' & @CRLF & _
                            'click OK to exit')
                            $finished = True
                        Else

    MsgBox(0,"StdoutRead()",'this is the output from the cmd.exe:' & @CRLF & @CRLF & _
                             $s_Output & @CRLF & @CRLF & _
                            'because the "=|(<&>)|=" string is NOT present' & @CRLF & _
                            'we know that the command has NOT finished' & @CRLF & _
                            'click OK to read more data from cmd.exe output stream')
                        EndIf

Until $finished = True

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

 

Thank you for the taking the time to explain the details.  I'm now armed with DOS knowledge in AutoIT.

May I ask if you can explain the statement you posted in comment #10?  The "Until 1 > 1 threw me off.  I've never seen that statement before.

Do (.....) Until 1 > 1

 

That's just a ploy to create a closed loop (infinite ) in order to continually perform a piece of code and carry out checks on the fulfillment of certain conditions ( since 1 > 1 will never occur , then the cycle would continue indefinitely )

the only way to exit the loop is via the command "exitloop" which is performed at our option on specific control conditions . (in that case the loop exits when this condition occurs : If StringInStr ($ output , ' succeeded on " 10.11.22.33:443 " ' )

another way is:

While 1

         ...........

    WEnd
Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...