23

I have a batch file:

arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
ipconfig /flushdns

How can I do these two commands on Windows XP, every 10 seconds?

migrated from stackoverflow.com Nov 7 '09 at 1:06

This question came from our site for professional and enthusiast programmers.

  • 3
    You can use Windows Scheduled Tasks to get it down to once a minute. – Jay Riggs Nov 7 '09 at 0:18
  • it is possible with Windows batch: see my solution below. the only problem is cpu usage... – kokbira Apr 25 '11 at 20:05
32

this makes a 10 sec. delay:

timeout /t 10

so try this:

 :loop
 arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
 ipconfig /flushdns
 timeout /t 10
 goto loop
  • 1
    The right answer for any version of Windows made in the last decade (including Windows XP specified in the question). Thanks :-) – woot Apr 9 '16 at 17:53
  • It actually waits -1 - 99 999 seconds (~~1day3,7h|windows7) - the number specified in /t OR for pressing any key - that can be turned off by sending /NOBREAK param :) – jave.web Oct 30 '16 at 14:42
19

Try this one:

:loop
arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
ipconfig /flushdns
ping localhost -n 11 > nul
goto loop

The ping command will execute for 10 seconds, and all the output will be redirected to the NUL device, meaning that you will see no output from the ping command. It works indeed as a "sleep" command would.

11
:top
arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
ipconfig /flushdns
sleep 10
goto top

Edit: As mentioned in the comments, SLEEP won't be available in a normal install. You'll need something like the Windows 2003 Resource Kit, or some other trick to simulate it (the ping trick Bruno mentions), and notes to do so can be found here.

  • 6
    sleep isn't included by default. You need the resource kit or something to get this? – marcc Nov 7 '09 at 0:19
  • What exactly do I need? – steven Nov 7 '09 at 0:21
  • It isn't? I could've sworn it was something I could count on, but I guess like choice it might not be included by standard. I'll look into it more. – Doug Kavendek Nov 7 '09 at 0:21
  • 1
    There's no need to install anything else on windows to "fake" a sleep command. Take a look at my answer for an example. – Bruno Reis Nov 7 '09 at 0:29
1

More precise solution ping unexistent host once and set timeout

ping 1.0.0.0 -n 1 -w 10000 >nul

But it generate parasite traffic

1

You can use the for and ping command:

@echo off
for /l %%a in (0,0,0) do (
arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
ipconfig /flushdns
ping -n 11 localhost >nul
)

You can use ping -n [secs+1] localhost >nul to wait a certain amount of time in seconds.

  • 1
    Why is this better than exactly the same ping command suggested in another answer? – RalfFriedl Feb 21 at 21:44
  • To be fair, this is the first answer that shows how to do an infinite loop without using goto. – Scott Feb 21 at 22:08
0

Cheat:

Use this command to pause the batch for 10 seconds

choice /n/t:c,<10>/c:cc

Now, place it in a never ending loop in the batch and voilà!

  • 1
    Or, if you want, download the resource kit or write a simple prog to sleep 10. My method however, will mean you can move it to another machine without having to download more software again. – Dan McG Nov 7 '09 at 0:22
  • I didn't think choice came with XP and later, or at least I haven't seen it on any recent installs. – Doug Kavendek Nov 7 '09 at 0:29
  • 1
    I'm running vanilla Vista Home Premium on this laptop and it works fine. – Dan McG Nov 7 '09 at 0:35
  • there is no "choice" for windows xp!!! – kokbira Apr 25 '11 at 19:27
0

Install Cygwin which will make sleep and cron available to you (among other things).

  • 2
    Overkill for what he is asking! – Bruno Reis Nov 7 '09 at 0:31
-1

below, it is both an ugly and a beautiful way, an Windows batch file - it consumes a lot of cpu time to do nothing, but it does what you want and is so nice.

SETLOCAL EnableDelayedExpansion

::in seconds
set time2stop=10

:loop1
arp -s 192.168.1.254 xx-xx-xx-xx-xx-xx
ipconfig /flushdns

for /f "tokens=1,2* delims=:" %%i in ("!time!") do (
  set hour1=%%i
  set min1=%%j
  set sec1=%%k
)

for /f "tokens=1* delims=," %%i in ("!sec1!") do (
  set isec1=%%i
)

:loop2

for /f "tokens=1,2* delims=:" %%i in ("!time!") do (
  set hour2=%%i
  set min2=%%j
  set sec2=%%k
)

for /f "tokens=1* delims=," %%i in ("!sec2!") do (
  set isec2=%%i
)

set /a delta=3600*(!hour2!-!hour1!)+60*(!min2!-!min1!)+(!isec2!-!isec1!)
if !delta! geq !time2stop! goto end2

goto loop2

:end2

@echo on

goto loop1
  • another way is to do some loop sequence, for example repeating ( for /l %%i in (1,1,65535) do set nothing=0 ) some times and "calibrating" "how many times" you have to repeat testing to result in 10 seconds, hahaha. but it is the ugliest way. – kokbira Apr 25 '11 at 20:10
  • you can also create some Windows batch to use as functions (one to split time, one to do the waiting...) but it will be more complex... – kokbira Apr 25 '11 at 20:13
  • I think it will cause a bug if you are using it from 23:59:51 to 23:59:59 - in that case, the batch will wait forever... some "if's" would solve the problem... – kokbira Apr 25 '11 at 20:17
  • if you want to test for a fixed time (not a time+10sec) you can simply do "if %time% geq 10:30 do goto end". the solution became complex because the sum... – kokbira Apr 25 '11 at 20:21
  • It's actually horrible. First of all, busy waiting. wasting CPU cycles unnecessarily is never a good idea (unless on embedded hardware). This also doesn't appear to work correctly around midnight. – Joey Apr 27 '11 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy