Skip to content

Instantly share code, notes, and snippets.

@joetde
Last active August 29, 2015 14:01
Show Gist options
  • Save joetde/6f33f6d687b60a1621c7 to your computer and use it in GitHub Desktop.
Save joetde/6f33f6d687b60a1621c7 to your computer and use it in GitHub Desktop.
Get opened TCP sockets
FILE *fp;
char cmd[1024];
char out[1024];
sprintf(cmd, "lsof -p %d 2> /dev/null | grep TCP | awk '{print $9}'", getpid());
fp = popen(cmd, "r");
if (fp == NULL) {
printf("Failed to run command\n");
exit(1);
}
while (fgets(out, sizeof(out)-1, fp) != NULL) {
printf("%s", out);
}
pclose(fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment