Skip to content

Instantly share code, notes, and snippets.

@stavrossk
Forked from zhouhao/Get IP by Hostname
Created February 19, 2013 21:25
Show Gist options
  • Save stavrossk/4990092 to your computer and use it in GitHub Desktop.
Save stavrossk/4990092 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include<netdb.h>
#include<arpa/inet.h>
int main(int argc, char **argv) {
struct hostent *host;
char str[100];
char addr[INET_ADDRSTRLEN];
char **p;
int c;
while (1){
c=0;
gets(str);
printf("%s\n",str);
if ((host=gethostbyname(str))==NULL){
perror("host");
break;
}
p=host->h_addr_list;
for (;*p!=NULL;p++){
c++;
inet_ntop(AF_INET, *p, addr, INET_ADDRSTRLEN);
printf("%d:%s\n", c, addr);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment