D
Daniel Berger
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 <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#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
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 <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#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