69

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

6 Answers 6

97

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
34

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 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.
    – ElMatador
    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 :) 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.
    – DavidPostill
    May 22, 2016 at 8:21
  • is not legal i ask to delete it plz
    – user466755
    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. 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.
    – LPChip
    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. 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.