0

Just as title says. I want to get a list of all IPs connected to my pc. Not the router and not the PCs on the network. Only the IPs which im connected to. I know there is a way in C# but how do you do it in C.
I am doing this on a linux.

OverloadedCore
  • 341
  • 4
  • 19

2 Answers2

0

This comment links to a program which parses reads from:

/proc/net/tcp
/proc/net/udp
/proc/net/raw
Community
  • 1
  • 1
Ynon
  • 294
  • 1
  • 7
0

You are asking for your IP addresses. To get a list of IP addressess per interface, you can use the ifconfig built-in linux functionality, that will output the data and then you can parse it.

Credit to this answer, and based on it, you can use the following code to print each address in a new line:

Before you run this, use locate ifconfig and set the path in the string accordingly.

Note my comment: First check that this command outputs the wanted output for you via the terminal!

#include <stdio.h>
#include <stdlib.h>


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

  FILE *fp;
  char ip[15];

  /* Open the command for reading. */
  fp = popen("ifconfig | grep \"inet addr\" | cut -d\":\" -f2 | cut -d\" \" -f1", "r");

  /*First check that this command outputs the wanted output for you*/
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit(1);
  }

  /* Read the output a line at a time - output it. */
  while (fgets(ip, sizeof(ip)-1, fp) != NULL) {
    printf("%s", ip);
  }

  /* close */
  pclose(fp);

  return 0;
}
Community
  • 1
  • 1
Ofer Arial
  • 1,054
  • 8
  • 20