APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Determining clients IP address by service and PID

© November 2004 BigDumbDinosaur
Search Keys: network

Attached is a simple program that you may add to your collection of stuff for OSR5. I wrote this thing several years ago for use with a program where I needed to know a client's IP address, given the TCP service on which they were connected and the program's PID (to differentiate when more than one session was open on the same service). I recently cleaned it up so it could be used from the command line.

The command line syntax is:

    getrip <service> <PID>

where <service> is a TCP service defined in /etc/services (port numbers will not work) and <PID> is the process ID of a program that is serving a client attached to >service>. If both <service> and <PID> are found to be attached together, getrip will print the remote client's IP address to STDOUT and return 0 to the caller. Otherwise, getrip will return 1 to the caller and print suitable diagnostics to STDERR.


    getrip telnet 12345

A copyright statement will be displayed if you type what getrip. Invoking getrip with no arguments will display some help. Recommended perms are 755 and ownership should be bin:bin. The lsof utility must be present on the system and accessible in the execution environment.

source getrip.c
SCO coff getrip_coff
SCO ELF getrip


get IP addr associated with port & PID -- 10/1999, wjb

grip port pid

remote IP printed on STDOUT


#include <stdio.h>

#define S_BUF           192
#define S_IPADDR        15
#define S_PORT          16

char    *iam;

void    usage(void);


void main(int argc,char *argv[]) {

    // argv[1] = port
    // argv[2] = caller's PID

        FILE    *f_lsof;
        char    *lsofcmd="lsof -n -T -w -i tcp:";
        int     bp=0;
        int     ip=0;
        int     oc;
        char    buffer[S_BUF+1];        // input buffer
        char    ipaddr[S_IPADDR+1];     // IP address


    if (argc != 3) {

    sprintf(buffer,"%s%s | grep %s | grep 0u",
        lsofcmd,argv[1],argv[2]);               // construct pipe command
    if ((f_lsof=popen(buffer,"r")) == NULL) {
        fprintf(stderr,"%s: couldn't pipe to lsof",iam);
    while ((oc=getc(f_lsof)) != EOF && (oc != '\n')) {
    if (bp == 0) {
            "%s: %s: no such process attached to %s\n",
    while (*(buffer+bp)) {
        if (*(buffer+bp++) == '>') {
            while (*(buffer+bp)) {
                if (*(buffer+bp) == ':') {
    fprintf(stderr,"%s: lsof output grabled: can't get IP address for %s\n",

/* display usage help */

void usage(void) {
    fprintf(stderr,"usage: %s port PID\n\n",iam);
        "    port  TCP port to interrogate, such as SMTP or POP3\n",
        "    PID   process ID of process bound to TCP port\n\n",
        "IP address, if any, is written to STDOUT.  Returns 0 if a matching\n",
        "IP address is found, 1 if not.\n\n",
        "The `lsof' utility must be installed on your system in order for\n",
        "this program to function.  `lsof' may be downloaded from SCO's\n",
        "Skunkware FTP site (ftp2.sco.com).\n");

/* copyright declaration for `what' command */

const char *what[]={
           "@(#)get remote IP address on TCP port V1.0 -- 10/16/1999",
           "@(#)                                  V1.1 -- 02/27/2004",
           "@(#)type `getrip' w/no arguments for command line usage",
           "@(#)Copyright (C)1985-2004 by BCS Technology Limited.  All rights",
           "@(#)reserved.  Permission granted to use, copy and/or  distribute",
           "@(#)this software,  providing proper attribution is given and all",
           "@(#)copyright notices remain intact.  This software is  *NOT*  in",
           "@(#)the public domain and has not been GPLed.",
           "@(#)!!! CAUTION:  USE AT YOUR OWN RISK !!!",
           "@(#)BCS Technology Limited does  not  accept  responsibility  for",
           "@(#)anything bad that might occur as a result of using this soft-",
           "@(#)ware.  Again, USE AT YOUR OWN RISK!",

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Determining clients IP address by service andPID

Inexpensive and informative Apple related e-books:

Take Control of Pages

Take control of Apple TV, Second Edition

Take Control of Numbers

Digital Sharing Crash Course

Sierra: A Take Control Crash Course

More Articles by © BigDumbDinosaur

---November 21, 2004

That code should work on any Unix/Linux that has lsof (most modern Unixes do) and since it is basically just parsing lsof's output, you could easily rewrite it in sh or perl or almost anything..


"...you could easily rewrite it in sh or perl..."

You could, but writing in C gives you many opportunities to decipher the cryptic diagnostics that the compiler belches out when you screw up. <Grin> Besides, if I don't write something in C now and then, I'll forget how to do it and might end up having to resort to using Visual Basic.

Anyhow, eventually I'll get around to writing getrip so it doesn't requires lsof -- just as soon as I get about 500 other programming projects out of the way. Would you believe one of them is for the Commodore 64? Which speaking of diagnostics, watching the 6502 assembler cough its cookies when it hits some bad source code is really entertaining...


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Get your facts first, and then you can distort them as much as you please. (Mark Twain)

Linux posts

Troubleshooting posts

This post tagged:






Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode