How do I output my host’s IP addresses from a C program?

后端 未结 8 563
猫巷女王i
猫巷女王i 2020-12-03 21:19

I need to display all the IP addresses from my local computer, using the C language. How can this be done?

相关标签:
8条回答
  • 2020-12-03 21:46

    You need POSIX function getaddrinfo() - it returns linked list of all IP addresses.

    See man getaddrinfo for details and examples.

    0 讨论(0)
  • 2020-12-03 21:48

    How about you simply cheat and look at the source of /sbin/ifconfig/ ? Nothing wrong with standing on the shoulders of other giants...

    0 讨论(0)
  • 2020-12-03 21:56
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    /*
     * Who sez?
     * http://blog.stackoverflow.com/2010/01/stack-overflow-where-we-hate-fun/
     */
    int main(int argc, char *argv[])
    {
      int status;
      const char * const cmd =  /* die from END is too chatty */
        "/sbin/ifconfig -a | \
         perl -lne \
           'print $1 if /inet6? addr:\\s*(\\S+)/; \
            END { $. > 0 or \
                    warn(\"no output from ifconfig\\n\"), \
                    exit 1; }'";
    
      status = system(cmd);
      if (status < 0) {
        perror("system");
        return 1;
      }
      else if (status != 0) {
        const char *extra;
        status = WEXITSTATUS(status);
        extra = status == 127 ? " (is /bin/sh ok?)" : "";
        fprintf(stderr, "%s: command failed with status %d%s\n",
                argv[0], status, extra);
      }
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-03 22:02

    Your question might be imprecise but I am not sure why everyone is breaking your chops.

    I think you are asking the basics in which case you probably want is getifaddrs. The man page has a little example program.

    You can also get similar info using the SIOCGIFCONF option with ioctl(). There is some sample code on here and the web.

    If you search around for these and similar terms you will find this question has been asked in various forms before. You have to dig around a bit.

    Also note, these will not give you the public facing IP of your network if you are behind NAT.

    0 讨论(0)
  • 2020-12-03 22:03

    Not a complete solution yet, but take a look in /proc/net!

    • dev lists the available interface devices by name,
    • route lists some routes, as does ipv6_route,
    • arp lists devices in the actual routing table (does not include localhost).

    Not as high-tech as the other solution, but it can be done with simple file reading. Linux specific, though.

    0 讨论(0)
  • 2020-12-03 22:03
    $ sudo ifconfig | grep 'inet addr' | cut -d':' -f2 | cut -d' ' -f1
    213.xx.xxx.xx
    192.168.xx.x
    127.0.0.1
    

    And you can put that into popen():

    /* not tested */
    ph = popen("sudo ifconfig | grep 'inet addr' | cut -d':' -f2 | cut -d' ' -f1", "r");
    while (fgets(buf, sizeof buf, ph)) {
        /* ip address, in nul-terminated string format, is in `buf` */
    }
    pclose(ph);
    
    0 讨论(0)
提交回复
热议问题