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.
Asked
Active
Viewed 129 times
0
-
Are we talking Linux or Windows OS? – Ofer Arial Jan 30 '17 at 18:37
-
@OferArial i am talking about linux - let me add that in with an edit – Jan 30 '17 at 18:39
-
I tried giving a solution for linux down below. – Ofer Arial Jan 30 '17 at 20:34
2 Answers
0
This comment links to a program which parses reads from:
/proc/net/tcp
/proc/net/udp
/proc/net/raw
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,129
- 1
- 10
- 25