Reputation: 3
I really have looked at multiple examples here, but I cannot get this to work - all the examples have a situation where you just call a command that actually does end, I need to keep mine running. This is what I need to do.
To configure a network device, I have the option to do so with an arp
command, and a ping command, but these need to be executed in the same command prompt, but each device needs to be in it's own window.
I need to run arp -s 192.168.5.5 00-CC-CC-CC-CC-CC
and when that is finished, I need to run ping -l 408 -t 192.168.5.5
.
These two commands have to run in the same command window. I would like to open lets say 30 of these at the same time, arp'ing and pinging the same address in that specific window, but different addresses in each other window (I hope this makes sense).
right now, I can get it to run, but it waits for the first command to finish, before starting the second. I have used the START
, but that does not work either.
Yes, I guess I could create 30 different batch files, but that would defeat the object, as I need to configure over 500 devices, so creating 500 batch files would take just as long; unless a batch file can create all the required batch files for me?
So, I have an excel spreadsheet that creates all the lines I need in the batch file, and I was hoping to use this:
start cmd.exe /K arp -s 192.168.5.5 00-40-8C-D4-FB-BC && ping -l 408 -t 192.168.5.5
start cmd.exe /K arp -s 192.168.5.6 00-40-8C-D4-F9-A9 && ping -l 408 -t 192.168.5.6
start cmd.exe /K arp -s 192.168.5.7 00-40-8C-D4-FB-F6 && ping -l 408 -t 192.168.5.7
start cmd.exe /K arp -s 192.168.5.8 00-40-8C-D4-FC-70 && ping -l 408 -t 192.168.5.8
start cmd.exe /K arp -s 192.168.5.9 00-40-8C-D4-FB-F4 && ping -l 408 -t 192.168.5.9
start cmd.exe /K arp -s 192.168.5.10 00-40-8C-D4-FB-F8 && ping -l 408 -t 192.168.5.10
So using the above, only the first command actually runs. If I set it to ping only say 300 times, it would switch to the next, but again this would defeat the object because I need to do 30 devices at once, so waiting for each to finish first would take forever.
Perhaps I am totally on the wrong track here, perhaps someone has a far better idea? Bare in mind, that each of the above commands has to run in its own command prompt window, otherwise the arp command fails if you try to do too many at the same time.
For clarity also, I need to run the arp command, and when that is complete run the ping command
Upvotes: 0
Views: 15434
Reputation: 7551
You need to escape your &&s, and maybe change to just &:
start cmd.exe /K arp -s 192.168.5.5 00-40-8C-D4-FB-BC ^& ping -l 408 -t 192.168.5.5
But, escaping in cmd can get tricky very quickly. I recommend you create a single DoPing.cmd with everything you need:
arp -s %1 %2
ping -l 408 -t %1
And call it from your generated cmd:
start cmd.exe /K DoPing.cmd 192.168.5.5 00-40-8C-D4-FB-BC
Of course, this means you need to deploy DoPing.cmd to wherever you're running.
Upvotes: 1
Reputation: 746
On problem is that you need to quote the arguments being passed to CMD. Your original is :
start cmd.exe /K arp -s 192.168.5.5 00-40-8C-D4-FB-BC && ping -l 408 -t 192.168.5.5
This syntax will launch a CMD window and pass the ARP command to that window as an argument, but not the && ping portion. Instead, that is treated as the command to run after the START.
To correct this we can add quotes that will explicitly pass the arp and ping as a single argument to the CMD.
start cmd.exe /K "arp -s 192.168.5.5 00-40-8C-D4-FB-BC && ping -l 408 -t 192.168.5.5"
Additionally, I see it suggested that the start command should always be provided a title, even if the title is null. So....
start "" cmd.exe /K "arp -s 192.168.5.5 00-40-8C-D4-FB-BC && ping -l 408 -t 192.168.5.5"
Of course you have further options. The && runs the second command only if the first returns successful. So in theory & could be used to ensure that the second command always runs. Then again there's no reason to both pinging if the arp call fails so && works to...
I assume you are leaving the new CMD windows open on purpose so you can see the results, but you could always use a /c instead of /k to auto close them.
As a note with the /c. You mention in a response to another answer that hitting control+c kills all windows. This is normal behavior, but running CMD with a /c should make the control+c only apply to the current window. Changing the following line in the other answer:
start %%b.bat
to...
start cmd /c %%b.bat
(or something similar) should let you use CTRL+C in individual windows spawned by the loop.
Upvotes: 0
Reputation: 67216
Let's assume that your data is in a text file this way:
192.168.5.5 00-40-8C-D4-FB-BC
192.168.5.6 00-40-8C-D4-F9-A9
192.168.5.7 00-40-8C-D4-FB-F6
192.168.5.8 00-40-8C-D4-FC-70
192.168.5.9 00-40-8C-D4-FB-F4
192.168.5.10 00-40-8C-D4-FB-F8
The Batch file below create multiple Batch files, each one with the required commands, and start they. Each created Batch file deletes itself after finished its business.
@echo off
for /F "tokens=1,2" %%a in (theDataFile.txt) do (
echo @echo off > %%b.bat
echo arp -s %%a %%b >> %%b.bat
echo ping -l 408 -t %%a >> %%b.bat
echo del %%b.bat ^& exit >> %%b.bat
start %%b.bat
)
Upvotes: 1
Reputation: 16077
Try quotes around your whole argument and also maybe use & rather than &&
eg
start cmd.exe /K "arp -s 192.168.5.5 00-40-8C-D4-FB-BC & ping -l 408 -t 192.168.5.5"
Upvotes: 0