IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
    
     Home      Products      Services & solutions      Support & downloads      My account     
 
developerworks > My developerWorks >  Dashboard > AIX > ... > AIXV53Howtos > AIXV53showlsnrs
developerWorks
Log In   View a printable version of the current page.
Overview Connect Spaces Forums Wikis
AIXV53showlsnrs
Added by OneSkyWalker, last edited by OneSkyWalker on Nov 06, 2009  (view change)
Labels: 
(None)

How to display processes which are communicating via TCP/IP connections

A sample showlsnrs Korn shell script is available which will display all TCP connections established to/from a specified host and display the process communicating via each of those connections.:

surveyor:/ # /usr/local/sbin/showlsnrs 9.76.206.50
Active Internet connections (including servers) to/from IP address 9.76.206.50
PCB/ADDR Proto Recv-Q Send-Q  Local Address      Foreign Address    (state)        PID     USER    STIME CMD
70e24210 tcp4       0      0  9.72.10.19.22      9.76.206.50.1956   ESTABLISHED  20210  pittman 14:04:17 sshd: pittman@pts/0
70e1fe10 tcp4       0    280  9.72.10.19.22      9.76.206.50.1958   ESTABLISHED  28916     root 14:06:23 sshd: root@pts/1
surveyor:/ #

The sample shell script has been tested and works correctly on AIX V5.3 TL6 SP1 and AIX V6.1 TL1 SP4.

Note

The sample showlsnrs Korn shell script illustrates how to do the following in the Korn shell:

  1. Use the AIX kdb command to determine the process communicating via an established TCP connection.
    #
    # Define function to use kdb to find process ID associated with a PCB control block
    #
    getproc()
      {
      echo "sockinfo $1 tcpcb" | kdb | grep "^pvproc" | read pvproc procname active hex1 hex2 rest
      echo $((16#$hex1))    # Convert PID from hex to decimal and return it
      }
    

    Because the AIX kdb command makes use of the /dev/mem special file, it requires root privilege.

    The PCB control block address (required as a parameter to the sockinfo subcommand of kdb) can be obtained using the AIX netstat command:

    netstatcmd='netstat -Aan'       # AIX netstat command to display network connections
    established='ESTABLISHED'       # State of connection when established
    $netstatcmd | grep $ipaddr | while read netline
    do
      set - $netline        # Parse netstat output line into positional parms
      pcbaddr=$1            # PCB control block addr is first token
      eval "connstate=\$$#" # Connection state is last token
      if [ "$connstate" == $established ]
      then
        pid=$(getproc $pcbaddr)     # Extract process ID from PCB control block
    
  2. Convert a hexadecimal number to a decimal number.
      echo $((16#$hex1))    # Convert PID from hex to decimal and return it
    

    The Arithmetic evaluation in the Korn shell or POSIX shell article is intended to document this technique, but as of 11/5/2009, the documentation there is not clear. Feedback has been submitted to request that the article be revised so it is clearer.

  3. Use the Korn shell eval special built-in command to compute the name of a shell environment variable before setting
      eval "hdr$line='$netline'"       # Save header line
    

    or referencing

      eval "connstate=\$$#"       # Connection state is last token
    

    the variable.

  4. Determine the length of a character string in a shell environment variable
      linelng=${#netline}           # Get length of netstat output line
    

    The Parameter substitution in the Korn shell or POSIX shell article documents this technique

    Please help!

    There might well be better ways of performing the operations listed above in the Korn shell. Please use the Add Comment link at the bottom of the page to inform others of better Korn shell techniques than those described above. Thanks!

    (Note: Until you sign up and log in (using links in the upper right corner of this web page), you will not see the Add Comment link and you can not add a comment.)

The contents of this web page solely reflect the personal views of the authors and do not necessarily represent the views, positions, strategies or opinions of IBM or IBM management. Please use the Add Comment link at the bottom of the page to provide feedback. Note: Until you log in (using the link in the upper right corner of this web page), you will not see the Add Comment link and you can not add a comment. If you do not already have an IBM ID, use the Register Now link on the sign in page to obtain one. Registration is quick and easy.


 
    About IBM Privacy Contact