Request batch file help

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
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?
8 answers Last reply Best Answer
More about request batch file help
  1. You could always just use an interval with netstat. E.g.

    netstat 10

    would repeat the command every 10 seconds.
  2. 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 :(

  3. You have to do it as:

    cmd/k "netstat 10"
  4. 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"
    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!
  5. Best answer
    The trick is to use the /S switch with cmd to modify the way that quotes are processed:
    cmd /s /k "netstat -n | cmd /k find "4000""
  6. Ijack said:
    The trick is to use the /S switch with cmd to modify the way that quotes are processed:
    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 :)

  7. Best answer selected by kruck.
  8. Oops. That second cmd /k was a mistake. But I just wanted to see if you'd spot that.;)
Ask a new question

Read More

Programming netstat Command Prompt Apps