Results 1 to 3 of 3

Thread: bash script to print a list of established connections

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Beans
    Hidden!

    Thumbs down bash script to print a list of established connections

    I looked on the net for this and couldn't find anything suitable. This is perfect if you have something like conky and want to see just a list of your established internet connections without all the fluff in between:
    Code:
    #!/bin/bash
    ##############################################################################
    # A simple bash script to quickly show established connections
    
    x="0"
    for i in `netstat -n | grep "ESTABLISHED"`; do
     x=$((x + 1))
      if [ $x == 5 ]; then
       if [[ $i != *'127.0'* ]] && [[ $i != *'192.168'* ]]; then
        echo "$i"
       fi
    elif [ $x == 6 ]; then
       x="0"
     fi
    done
    The output will look something like this:
    Code:
    72.127.xxx.xxx
    42.78.xxx.xxx
    98.54.xxx.xxx
    ...etc, etc
    Enjoy

  2. #2
    Join Date
    Jun 2005
    Beans
    4

    Re: bash script to print a list of established connections

    Hello, i know the last post was posted some time ago, but i was wondering how the script would have to look like, if i would want to add "lsof -i :port" process information behind the established ports and ip addresses?
    Someone who can help me out here?

    Example:
    x.x.x.x:port/COMMAND:USER:NODE:NAME (maybe, just the command+name)

    thanks,
    snitride
    Last edited by snitride; March 24th, 2012 at 07:35 PM.

  3. #3
    Join Date
    Jun 2005
    Beans
    4

    Re: bash script to print a list of established connections

    something like this, it work, but of course its not optimal..
    you will have to run it with sudo/root permissions.
    Also it will only show one line of lsof output for every port..
    Code:
    #!/bin/bash
    ##############################################################################
    # A simple bash script to quickly show established connections
    x="0"
    for i in `netstat -n | grep "ESTABLISHED"`; do
    x=$((x + 1))
     if [ $x == 5 ]; then
      if [[ $i != *'127.0'* ]] && [[ $i != *'0.0'* ]]; then
       FORLSOF=`echo $i | cut -f 2 -d :`
       cut1=`lsof -i :$FORLSOF | tail -1 | tr -s ' ' | cut -d' ' -f1`
       cut2=`lsof -i :$FORLSOF | tail -1 | tr -s ' ' | cut -d' ' -f9`
       echo "$i $cut1 $cut2"
      fi
    elif [ $x == 6 ]; then
      x="0"
    fi
    done
    Anyone got some further ideas, howto to optimize the code?
    Last edited by snitride; March 26th, 2012 at 07:23 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •