Sign in with
Sign up | Sign in
Your question
Solved

Request batch file help

Last response: in Applications
Share
August 3, 2010 3:40:07 AM

I'm trying to write a batch file that does a few simple(?) things. My fundamental goal is to open a command window, perform a netstat command, then use PAUSE to wait for any key, then run the netstat command again in the same command window. Repeatedly, until I close the window.

When I just used Netstat, the command window self-terminated, so I found
cmd /k netstat would keep it open. But that doesn't help, because it only runs once.

So I tried a loop, and so far my syntax has been unsuccessful, I think because I need to use the cmd /k the first time, and a plain netstat command the second time... but I can't figure out how to get the batch file to insert additional netstat commands in the same command window.

Something like:
cmd /k netstat
PAUSE
:numbers
netstat
PAUSE
goto :numbers

but again, the subsequent netstat commands don't show when I hit a key and the command window is active.

Also, I'm not at that point yet, but I'm hoping this will remain linked to the command window; I will be bouncing to another program to perform activities, then will want to continually bounce back to the command window to check the IP address list.

Can anyone give a hand?
Thanks!

More about : request batch file

a b L Programming
August 3, 2010 6:32:15 AM

You could always just use an interval with netstat. E.g.

netstat 10

would repeat the command every 10 seconds.
m
0
l
August 3, 2010 12:41:41 PM

the thing that has me stuck is pushing the second netstat to the command window; if I just use the cmd /k then it opens a new window each time, which isn't what I want- once that command window is open, how do I make that netstat 10 active?

cmd /k netstat
netstat 10

doesn't refresh the netstat every 10 seconds, it just does it once :( 

Thanks!
m
0
l
Related resources
a b L Programming
August 3, 2010 2:22:08 PM

You have to do it as:

cmd/k "netstat 10"
m
0
l
August 10, 2010 1:55:27 AM

awesome, that worked; can I ask for one more piece of advice?

To narrow down what gets returned, I want to look for only IP addresses that include a specific number. I can do this with the hand-entered command
netstat -n | find "4000"

trying to figure out the syntax for those double quotes is killing me; I've tried things like
cmd/k "netstat -n | find """4000""" 10"
and
cmd/k "netstat -n | find " & chr(34) & "4000" & chr(34) & " 10"
cmd/k "netstat -n | find " & chr(34) & """4000""" & chr(34) & " 10"
cmd/k "netstat -n | find " & """ & "4000" & """ & " 10"

but nothing seems to work.

Many thanks!
m
0
l

Best solution

a b L Programming
August 10, 2010 8:28:54 AM

The trick is to use the /S switch with cmd to modify the way that quotes are processed:
  1. cmd /s /k "netstat -n | cmd /k find "4000""
Share
August 15, 2010 1:58:25 PM

Ijack said:
The trick is to use the /S switch with cmd to modify the way that quotes are processed:
  1. cmd /s /k "netstat -n | cmd /k find "4000""



Thanks iJack, that got me sooo close I was able to finish it off by trial and error; here is the final version I used:

cmd /s /k "netstat -n 5 | find "4000""

Marking you answer as correct :) 

Thanks,
kruck
m
0
l
August 15, 2010 1:58:36 PM

Best answer selected by kruck.
m
0
l
a b L Programming
August 15, 2010 3:48:22 PM

Oops. That second cmd /k was a mistake. But I just wanted to see if you'd spot that.;)
m
0
l
!