How to find which port a service is using by "grep" the result of netstat output on windows 7?

07
2014-07
  • Shuman

    i want to find which port a service is using. i know i can use

    netstat -anb 
    

    to show all the protocols and all applications. but how can i filter the result, eg. if the program i'm trying to identify is "muster"

    netstat -a -a -b | find /i "muster"
    

    but it only returns the application line

      Muster_Dispatcher_8
      Muster_Dispatcher_8
      Muster_Dispatcher_8
      Muster_Dispatcher_8
      Muster_Dispatcher_8
    

    because programe name and port number are not on the same line.

    ...
     [svchost.exe]
      TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING
     Can not obtain ownership information
      TCP    0.0.0.0:5800           0.0.0.0:0              LISTENING
     [System]
      TCP    0.0.0.0:5900           0.0.0.0:0              LISTENING
     [System]
      TCP    0.0.0.0:5938           0.0.0.0:0              LISTENING
     [TeamViewer_Service.exe]
      TCP    0.0.0.0:7955           0.0.0.0:0              LISTENING
     [KMPProcess.exe]
      TCP    0.0.0.0:9780           0.0.0.0:0              LISTENING
      Muster_Dispatcher_8
     [System]
      TCP    0.0.0.0:9781           0.0.0.0:0              LISTENING
      Muster_Dispatcher_8
     [System]
      TCP    0.0.0.0:9783           0.0.0.0:0              LISTENING
      Muster_Dispatcher_8
     [System]
      TCP    0.0.0.0:9790           0.0.0.0:0              LISTENING
      Muster_Dispatcher_8
     [System]
      TCP    0.0.0.0:9791           0.0.0.0:0              LISTENING
      Muster_Dispatcher_8
     [System]
    ...
    

    how can i tell find or findstr to return lines in context, meaning ,if line n is a match, eg. return line n-1,n,n+1 ? ( include the surrounding lines of the matching line )

    can this be done in simple command prompt , or has to be in a shell script ? or has to use powershell ? thanks !

    the intention here is to get multiple lines of output from the command such as

    ...
    TCP    0.0.0.0:9791           0.0.0.0:0              LISTENING   Muster_Dispatcher_8
    ...
    
  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    osx - How can I close a port (seen open on netstat)?
  • Questioner

    I used netstat and I see some ports open that I want closed. How do I close them?

    The computer is a Mac running OS X


  • Related Answers
  • epatel

    Try using

    lsof | grep portnumber

    to get the process that has it opened.

    You could also use the -i option to let lsof do the filtering for a TCP port

    lsof -i TCP:portnumber