From: Daniel Berger Date: 2007-07-19T02:13:00+09:00 Subject: Re: Listing all IP addresses on an multihomed system On 7/18/07, Francis Cianfrocca wrote: > On 7/18/07, Daniel Berger wrote: > > > > Hi all, > > > > I'm wondering if there's a way to list all IP addresses on a > > multihomed system using Ruby's Socket API. I actually want this for > > the sys-host library (at http://www.rubyforge.org/projects/sysutils). > > It's C, but I figure I can reverse engineer the Ruby code back to C. > > > > Here's the equivalent of what I'm currently doing (in C): > > > > #include > > #include > > #include > > #include > > > > #define BUF 256 > > > > int main(){ > > struct hostent* h; > > char hostname[BUF]; > > char ip[BUF]; > > > > if(gethostname(hostname, BUF) != 0){ > > printf("gethostbyname failed\n"); > > return -1; > > } > > > > printf("Hostname: %s\n", hostname); > > > > h = gethostbyname(hostname); > > > > if(!h){ > > printf("gethostbyname failed\n"); > > return -1; > > } > > > > while(*h->h_addr_list){ > > printf("Addr: %s\n", inet_ntop(h->h_addrtype, *h->h_addr_list, > > ip, BUF)); > > *h->h_addr_list++; > > } > > > > return 0; > > } > > > > But, at least one user has reported that he has eth0 configured with a > > LAN IP and eth1 that has 5 attached IP addresses. The above code > > doesn't pick them up. > > > > Is there a pure Ruby solution? Is it just a matter of > > Socket.getaddrinfo(Socket.gethostname, 80)? Or, does anyone happen to > > know the C solution? > > > > Thanks, > > > > Dan > > > > > > > > Since you're obviously using Linux, how about just parsing the output of > ethtool? > Eh? No, the C code above works just fine on Solaris (with -lsocket and -lnsl added). Besides, I need the code to be as cross platform as possible (for Unix flavors, that is - I use a different approach on MS Windows). So, parsing system calls is out of the question. Regards, Dan