Thursday, October 02nd, 2008 | Author: Angelo

Below is command to find out ports which are in use using netstat & cut.

netstat -na | grep 'tcp\|udp' | awk '{print $4}' | cut -d: -f2 | sort | uniq -c | awk '{print $2}'

Below is description of each commands :: Netstat command is used to check all incoming and outgoing connections on linux server. Using Grep command you can sort lines which are matching pattern you defined. AWk is very important command generally used for scanning pattern and process it. It is powerful tool for shell scripting. Sort is used to sort output and sort -n is for sorting output in numeric order. Uniq -c this help to get uniq output by deleting duplicate lines from it.

Category: Linux Commands
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

15 Responses

  1. Just made a change in the post, The problem with the command was that while posting it initially i did not put the code tag around the command and so the the single quotes that should appear as ' was appearing as ‘ .

    So when you had copied the command earlier it was giving an error.

    That was my mistake hope it helps now.

  2. error:
    awk: ‘{print
    awk: ^ invalid char ‘?’ in expression

  3. The quotes arount the params for grep and awk are wrong and should be just simple ‘ instead of those fansy pansy ‘ ’.

    So for cut and past:
    netstat -nap | grep ‘tcp\|udp’ | awk ‘{print $4}’ | cut -d: -f2 | sort | uniq -c | sort -n

  4. try “netstat -putwlan” it is much more informative.

  5. Hi,
    some modifications:
    1. -p is not needed at netstat
    2. grep can be omitted if awk or syntax is used
    3. first sort can be omitted
    4. last sort sorts each column by numbers - this is probably better.

    netstat -na | awk ‘$1==”tcp” || $1==”udp” {print $4}’ | cut -d: -f2 | uniq -c | sort -k 1,1n -k 2,2n

    Regards,
    Abcuser

  6. 6
    redhatter 
    Monday, 6. October 2008

    A shorter alternative:
    lsof -i -n

  7. I am no script master, but always willing to learn, when it is useful to me.
    To me this was a futile and i guess a way of learning to use awk.

    The authors example, and most of the responders corrections, threw errors.

    Author example
    netstat -nap | grep ‘tcp\|udp’ | awk ‘{print $4}’ | cut -d: -f2 | sort | uniq -c | awk ‘{print $2}’

    awk: ‘{print
    awk: ^ invalid char ‘?’ in expression
    awk: ‘{print
    awk: ^ invalid char ‘?’ in expression
    (Not all processes could be identified, non-owned process info
    will not be shown, you would have to be root to see it all.)
    warning, got bogus unix line.

    Responders corrections
    Jondor correction
    netstat -nap | grep ‘tcp\|udp’ | awk ‘{print $4}’ | cut -d: -f2 | sort | uniq -c | sort -n
    awk: ‘{print
    awk: ^ invalid char ‘?’ in expression
    (Not all processes could be identified, non-owned process info
    will not be shown, you would have to be root to see it all.)
    warning, got bogus unix line.

    abcuser correction

    netstat -na | awk ‘$1==”tcp” || $1==”udp” {print $4}’ | cut -d: -f2 | uniq -c | sort -k 1,1n -k 2,2n

    awk: ‘==”tcp”
    awk: ^ invalid char ‘?’ in expression
    warning, got bogus unix line.
    bash: ==”udp”: command not found

    This is the only one that worked.
    netstat -putwlan

    I guess it is a learning process when it is all said and done. But bogus commands make reading these type of blogs untrustworthy, and i wonder if they were tested before being posted.

    PEACE

  8. scribe63,
    the problem is commands are correct but blog doesn’t put correct characters.

    So character ‘ or ’ is single quote and character ” is double quote. Replace this two characters with correct one from keyboard.

    [code]netstat -na | awk ‘$1==”tcp” || $1==”udp” {print $4}’ | cut -d: -f2 | uniq -c | sort -k 1,1n -k 2,2n[/code]

  9. WOW what a difference. Ok i stand corrected.

    Lesson learned, do not copy command from blog and run in shell or maybe even paste in text editor.

    LOL

    Thanks very much for the pointer abcuser and brining it to my attention, did not notice that subtle difference at all. I did change the quote characters and the commands do work.

    netstat -na | awk ‘$1==”tcp” || $1==”udp” {print $4}’ | cut -d: -f2 | uniq -c | sort -k 1,1n -k 2,2n

    1 111
    1 111
    1 631
    1 631
    1 868
    1 892
    1 895
    1 898
    1 5353
    1 5433
    1 32770
    1 32771
    1 44774

    I guess it a writers blog as oppose to a scripting language blog, if there is such a thing

    Thanks again abcuser

    PEACE

  10. Hello,

    Command netstat -na | awk ‘$1==”tcp” || $1==”udp” {print $4}’ | cut -d: -f2 | uniq -c | sort -k 1,1n -k 2,2n show all ports but it is every complicated to understand and recall
    command given in post is simple to understand also single quotes issue is corrected in post so you can copy command from blog and run in shell.

  11. I recently discovered this blog. I’ve had a similiar idea to your rewriting the ten commandments for some time now.

  12. 12
    davidavila 
    Saturday, 1. November 2008

    Keep up the good work! :)

  13. Thanks for the great tips.

  14. Thats an interesting article - your blog is really good i keep coming back here all the time keep it up!

  15. Thanks for the great tips.

Leave a Reply