76

How to run Windows OS cmd.exe multiple commands one after another, I use ncrack, commands

I manually open cmd.exe and I paste this code:

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f 

When pass1.txt is finished I paste manually to cmd.exe the second command, which contains the Pass2.txt etc...:

ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f 

then I paste manually to cmd, Pass.3txt

ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f 

How can I run all  commands automatically in a batch file, one after another and not all at the same time?

5
  • 2
    This question is upvoted and has upvoted answers, few people found it helpful. Please don't spoil it. Commented Sep 19, 2017 at 6:49
  • 3
    Why are you trying to delete this question and answers? Is it because it uses ncrack as example? Because ncrack targets particular IP? If so, the right thing to do is to change the example command, not to nuke the entire question. If you tell us your reasons we may work something out. Commented Sep 19, 2017 at 8:56
  • 1
    I replaced the actual IP address with <IPAddress> in case that is your concern as well. Commented Sep 19, 2017 at 11:26
  • 2
    Does this answer your question? Execute multiple commands with 1 line in Windows commandline? Commented Jan 3, 2020 at 20:23
  • Does this answer your question? Run multiple commands one after another in cmd o{[]|||||||}::;;::;;::;;::;;::;;::;;::> ♦♦♦ Commented Apr 6, 2022 at 0:29

6 Answers 6

101

Run multiple commands one after another in cmd

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file.

Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.

Execute command2 after execution of command1 has finished

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f & ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f & ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f 

Execute command2 only if execution of command1 has finished successfully

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f && ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f && ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f 

Execute command2 only if execution of command1 has finished unsuccessfully

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f || ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f || ncrack --user Admin -P pass3.txt <IPAddress>:3389 -oN good.txt -f 

Supporting Resources

0
36

Use below syntax in your cmd file.

call command1 call command2 . . call commandx 

Example:

call mvn install:install-file -Dfile=spring.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=spring -Dversion=1.0 -Dpackaging=jar call mvn install:install-file -Dfile=com.ibm.ws.prereq.jaxrs.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=com.ibm.ws.prereq.jaxrs -Dversion=1.0 -Dpackaging=jar call mvn install:install-file -Dfile=com.ibm.ws.runtime.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=com.ibm.ws.runtime -Dversion=1.0 -Dpackaging=jar call mvn install:install-file -Dfile=IMSConnection_Utilities.jar -DgroupId=com.td.tdi.creditProtection.webservice -DartifactId=IMSConnection_Utilities -Dversion=1.0 -Dpackaging=jar 
2
  • 2
    I mean ... that's exactly is the command I am trying to run multiple times Commented Oct 24, 2019 at 16:15
  • Clean. The call does not care if a command fails, it just runs the next call, precisely what I wanted. Commented Feb 24, 2023 at 1:28
7

You can enter both commands on the same line and separate them with either a single ampersand (which causes them to be run in sequence) or two ampersands (which introduces simple error checking: the second command only runs if the first one was successful)

e.g.:

ncrack --user Admin -P pass1.txt <IPAddress>:3389 -oN good.txt -f && ncrack --user Admin -P pass2.txt <IPAddress>:3389 -oN good.txt -f 

Source

3
  • I see JUICED_IT beat me to posting this solution :) Commented May 21, 2016 at 21:06
  • 2
    @MunteanCosminManim Changing your requirements after you've received answers is inappropriate, as it invalidates the answers you've already received. Commented May 22, 2016 at 8:21
  • is not legal i ask to delete it plz Commented Sep 19, 2017 at 7:15
6

Just add all the commands line by line in a batch file, and save the file as somename.bat.

Execute that batch file; all the commands would run sequentially in the order of their presence in the file.

How to execute the batch file through cmd:

path/to/the/directory/of/your/batchfile/somename.bat 
3
  • 1
    @MunteanCosminManim - They would be running one after another, not at the same time. I hope this is what you want. Commented May 21, 2016 at 19:47
  • @MunteanCosminManim Yes, this is how a batch file works. It executes a command and waits until the command is finished, then runs the next command. In order to execute a command and immediately continue to the next, you'll have to use the command call or start too. Commented May 21, 2016 at 20:57
  • No, "all the commands would run sequentially" is wrong. Try del and ren on like 50 files, the ren will throw error of duplicate names. Commented Jan 6, 2021 at 8:55
1

All I did was paste the commands.  For instance, to better my Internet connection I pasted:

ipconfig /flushdns ipconfig /registerdns ipconfig /release ipconfig /renew netsh int ip reset netsh winsock reset 

Press the right click on your mouse, and whether on cmd or powershell it automatically executes all of them in sequence.  You don't even have to press 'enter'. Simple! (the last one i.e 'netsh winsock reset'was the only one I had to press enter for as it did not run automatically)

0

Use ^& instead of & to escape the special meaning of & in Windows batch scripts. This allows the & operator to be correctly interpreted as part of the command.

start cmd /k cd /D "Client_Server_Parallel" ^& make -f "%MAKEFILE%" run_server 

You must log in to answer this question.