내 자신의 서버 ip주소를 알아내는 방법 그런데 로칼 아이피만 나옵니다.

kimyh의 이미지

내 자신의 서버가 연결돤 현재의 유동 아이피 주소를 알고자 아래와 같은 소스를 구하여 돌려보니 네트워크에 연결된 유동 아이피 주소가 나오는게 아니고 Local Loopback 주소인 127.0.0.1 이나옵니다.

실제 네트워크에 연결된 유동아이피 주소를 알려면 아래 소스의 어느부분을 수정해야 하는지요?
아시는분 도움좀 부탁 드립니다.

소스 ;

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/utsname.h>
 
char **my_addrs(int *addrtype);
 
int main(void)
{
   struct in_addr in;
   char **addr;
   int type;
 
   addr=my_addrs(&type);
 
   memcpy(&in.s_addr, *addr, sizeof (in.s_addr));
   printf("%s\n", inet_ntoa(in));
 
   return 0;
}
 
/* 
 *  Description : Current localhost IP 
 *  Argument    : addrtype  - type of address (INET4 or INET6) 
 *  Return      : the hostent structure or a NULL pointer 
 */ 
char **my_addrs(int *addrtype) 
{ 
    struct hostent  *hptr; 
    struct utsname  myname; 
 
 
    if (uname(&myname) < 0) 
        return NULL; 
 
    if ((hptr = gethostbyname(myname.nodename)) == NULL) 
        return NULL; 
 
    *addrtype = hptr->h_addrtype; 
 
    return (hptr->h_addr_list); 
}

ifconfig 결과;
eth1 Link encap:Ethernet HWaddr 00:02:B3:AF:BA:9F
inet addr:224.165.132.xxx Bcast:255.255.255.255 Mask:255.255.255.128
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:42666 errors:0 dropped:0 overruns:0 frame:0
TX packets:2505 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:3720870 (3.5 Mb) TX bytes:215677 (210.6 Kb)
Interrupt:20 Base address:0x8800 Memory:fc261000-fc261038

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:14 errors:0 dropped:0 overruns:0 frame:0
TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:900 (900.0 b) TX bytes:900 (900.0 b)

모지리의 이미지

코드를 올려 드리는것 보다는 찾는 방법을 올려드리는게 더 좋을듯 싶어서
방법만 드립니다. 우선 자신의 로컬 IP를 알고자 하면 몇가지 단서를 달아야
만 됩니다. 그냥 내 로컬 IP라는것은 너무 막연하게 되거든요.

1. 전체 인터페이스의 모든 IP를 알고 싶은지.
2. 특정 인터페이스의 모든 IP를 알고 싶은지.
3. 특정 인터페이스의 특정 IP를 알고 싶은지.

이중에 자신이 만들어 놓은 조건을 가지고 IOCTL이나 NETLINK를 이용해서
원하는 IP를 얻어 오시면 됩니다. 코드는 인터넷에 널려 있고 구굴에서
조금만 검색해봐도 손쉽게 찾으실수 있습니다.

kimyh의 이미지

알려주신대로 소스 구해서 잘 해결 했습니다.
다시한번 바쁘신중에 답변 주신데 정말 감사드립니다.


나를 고생 시키고 있는 놈
리눅스, php, js, html, 스크립트, 플래쉬... 쥑일넘들...

살며 그리고 사랑하며...
정보는 공유할때 그 가치가 있는것.....
나의 조그만 지식공유는 남에게 엄청난 기쁨을 안겨 준다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.