From: Francis Cianfrocca Date: 2007-07-19T00:35:01+09:00 Subject: Re: Listing all IP addresses on an multihomed system ------=_Part_102684_23844313.1184772903789 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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? ------=_Part_102684_23844313.1184772903789--