know my ip - Unix

This is a discussion on know my ip - Unix ; I am developing an application in which I need to know my ip address irespective of my internet connection(PPPoE, LAN, etc). Somebody told me that I can query a web server over internet to know my address. How can I ...

+ Reply to Thread
Results 1 to 16 of 16

Thread: know my ip

  1. know my ip

    I am developing an application in which I need to know my ip address
    irespective of my internet connection(PPPoE, LAN, etc).

    Somebody told me that I can query a web server over internet to know
    my address. How can I do thins in C ????.

  2. Re: know my ip

    asit wrote:
    > I am developing an application in which I need to know my ip address
    > irespective of my internet connection(PPPoE, LAN, etc).
    >
    > Somebody told me that I can query a web server over internet to know
    > my address. How can I do thins in C ????.


    First of all, you may have multiple IP addresses.

    The sanest way to do find your local IP address(es), IMO, is to use
    getifaddrs(). getifaddrs() is widely found on unix-like systems, though not
    part of an official standard. `grep -r /usr/include getifaddrs' for the
    interface, or google for the AIX man page.

    As for the method recommended to you, in short you would create a socket,
    connect to an address, then use getsockname(2) to acquire the local address
    used for the connection. That, ostensibly, would be your IP address at the
    time you called connect(2).


  3. Re: know my ip

    On Oct 12, 11:02*am, asit wrote:
    > I am developing an application in which I need to know my ip address
    > irespective of my internet connection(PPPoE, LAN, etc).
    >
    > Somebody told me that I can query a web server over internet to know
    > my address. How can I do thins in C ????.


    Your IP address is 127.0.0.1.

    If you want a useful answer, you need to explain your problem. All
    you've explained is how you plan to solve it. We can't tell whether
    your solution is right or wrong or how to fix it without knowing the
    problem.

    Why do you think you need to know your IP address?

    DS

  4. Re: know my ip

    >I am developing an application in which I need to know my ip address
    >irespective of my internet connection(PPPoE, LAN, etc).


    You get one IP address per interface (minimum), and net-capable
    systems that aren't connected to any network still likely have
    "127.0.0.1" as an IP address. However, I doubt that's the answer
    you want.

    Running "ifconfig -a" might give you the answer you want mixed in
    with some answers you don't. Or it might fail completely when that
    command doesn't exist on your system or you don't have permission
    to run it.

    Talking about "my ip address" is a lot like talking about "Pressing
    THE key with THE finger on THE keyboard", as if there is only one
    finger.

    >Somebody told me that I can query a web server over internet to know
    >my address.


    Not if you don't have Internet connectivity. Which web server?

    >How can I do thins in C ????.


    If you've got a network connection set up, getsockname() on the
    socket on your end of the connection gets you the address of your
    end of the connection.


  5. Re: know my ip

    asit wrote:
    > I am developing an application in which I need to know my ip address
    > irespective of my internet connection(PPPoE, LAN, etc).
    >
    > Somebody told me that I can query a web server over internet to know
    > my address. How can I do thins in C ????.


    If I understand right, what you want is not you own actual IP but the
    IP everybody sees from the internet. For example my IP is 10.0.0.31
    and my gateway is 10.0.0.2 . But if I enter http://whatismyip.com/ the
    page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    Is this what you want to find out??

  6. Re: know my ip

    On Oct 15, 9:17 am, danmat...@gmail.com wrote:
    > asit wrote:
    > > I am developing an application in which I need to know my ip address
    > > irespective of my internet connection(PPPoE, LAN, etc).

    >
    > > Somebody told me that I can query a web server over internet to know
    > > my address. How can I do thins in C ????.

    >
    > If I understand right, what you want is not you own actual IP but the
    > IP everybody sees from the internet. For example my IP is 10.0.0.31
    > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
    > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    > Is this what you want to find out??


    Yes...

  7. Re: know my ip

    On Oct 16, 11:11 am, asit wrote:
    > On Oct 15, 9:17 am, danmat...@gmail.com wrote:
    >
    > > asit wrote:
    > > > I am developing an application in which I need to know my ip address
    > > > irespective of my internet connection(PPPoE, LAN, etc).

    >
    > > > Somebody told me that I can query a web server over internet to know
    > > > my address. How can I do thins in C ????.

    >
    > > If I understand right, what you want is not you own actual IP but the
    > > IP everybody sees from the internet. For example my IP is 10.0.0.31
    > > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
    > > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    > > Is this what you want to find out??

    >
    > Yes...


    You can parse the output of:

    lynx -dump www.whatismyip.com | grep 'Your IP'

    or

    lynx -dump checkip.dyndns.org

    But this will stopt working if www.whatismyip.com or
    checkip.dyndns.org disappear or change.

    See http://www.go2linux.org/what-is-my-p...ess-with-linux
    for some other ideas.

    I don't know if the socket api can give you a better solution, but I
    think http://checkip.dyndns.org/ is quite safe.






  8. Re: know my ip

    On 16 oct, 12:11, asit wrote:
    > On Oct 15, 9:17 am, danmat...@gmail.com wrote:
    >
    > > asit wrote:
    > > > I am developing an application in which I need to know my ip address
    > > > irespective of my internet connection(PPPoE, LAN, etc).

    >
    > > > Somebody told me that I can query a web server over internet to know
    > > > my address. How can I do thins in C ????.

    >
    > > If I understand right, what you want is not you own actual IP but the
    > > IP everybody sees from the internet. For example my IP is 10.0.0.31
    > > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
    > > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    > > Is this what you want to find out??

    >
    > Yes...


    Try this, get_public_ip takes an array of at least 16 chars and
    returns a string with your public ip.

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #define REC_SIZE 1000

    int get_public_ip ( char * ip ){

    int sock;
    int ret, position= 0;
    struct hostent *host;
    struct sockaddr_in server_addr;
    char received[REC_SIZE], *curr_ip;

    /* Connect to HTTP Server at checkip.dyndns.org */

    host = gethostbyname("checkip.dyndns.org");

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) return -1;

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(80);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server_addr.sin_zero),8);

    if ( connect (sock, (struct sockaddr *)&server_addr, sizeof(struct
    sockaddr)) == -1) return - 1;

    /* Request Main Page */

    char request[]= "GET / HTTP/1.1\r\n\r\n";

    ret= send (sock, request, sizeof (request), 0);

    if (ret != sizeof (request)) return -1;

    /* Receive Page */

    while ( (ret= recv (sock, received + position , REC_SIZE - position,
    0)) > 0)
    position+= ret;

    if ( ret != 0) return -1;

    received[REC_SIZE -1]= '\0';

    close (sock);

    /* Search for "Current IP Address:" */

    curr_ip= strstr (received, "Current IP Address:");

    if (curr_ip == NULL) return -1;

    memset (ip, 0, 16);
    ret= sscanf (curr_ip, "%*[^0-9]%15[0-9.]", ip);
    ip[15]='\0';

    if ( ret != 1) return -1;

    return 0;

    };

    int main () {

    char ip[20];

    if (get_public_ip (ip)){

    puts ("Could not get current Public IP");

    } else {

    printf ("Public IP is: %s\n", ip);

    }

    return 0;
    }

  9. Re: know my ip

    On Oct 16, 7:11*am, asit wrote:

    > > If I understand right, what you want is not you own actual IP but the
    > > IP everybody sees from the internet. For example my IP is 10.0.0.31
    > > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
    > > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    > > Is this what you want to find out??

    >
    > Yes...


    Presumably, you are going to tell the IP to someone. So why not just
    connect to them and say "my IP is whatever IP you see this connection
    coming from". This ensure that you give them the correct answer for
    them to reach you.

    DS

  10. Re: know my ip

    In article ,
    wrote:
    ....
    >lynx -dump checkip.dyndns.org
    >
    >But this will stopt working if www.whatismyip.com or
    >checkip.dyndns.org disappear or change.


    Right. That's the crux of it. There doesn't seem to be anyway to do it
    that doesn't depend on knowing the name of something external.

    >See http://www.go2linux.org/what-is-my-p...ess-with-linux
    >for some other ideas.


    Interesting read, but all of them are just variations on the theme of
    checking something like whatismyip or dyndns.

    >I don't know if the socket api can give you a better solution, but I
    >think http://checkip.dyndns.org/ is quite safe.


    Absolutely. Unless/until they change it.

    Of course it then follows that the only thoroughly reliable ways to do
    this is to do it yourself - i.e., have some machine of your own be on
    the public net. One of the posters in the go2linux page had that
    situation - where he could get what he needed directly from ifconfig,
    because his machine was actually on the public net.

    P.S. Another way to go about this is to try to query the information
    from your own router - it obviously knows the external IP address. The
    trick is figuring out a good automated way to get it to divulge the
    information.


  11. Re: know my ip

    In article <076440ba-6bbf-4639-9009-4c42512e9275@y71g2000hsa.googlegroups.com>,
    David Schwartz wrote:
    >On Oct 16, 7:11*am, asit wrote:
    >
    >> > If I understand right, what you want is not you own actual IP but the
    >> > IP everybody sees from the internet. For example my IP is 10.0.0.31
    >> > and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
    >> > page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    >> > Is this what you want to find out??

    >>
    >> Yes...

    >
    >Presumably, you are going to tell the IP to someone. So why not just
    >connect to them and say "my IP is whatever IP you see this connection
    >coming from". This ensure that you give them the correct answer for
    >them to reach you.
    >
    >DS


    There are a lot of practical ways that that can fail.


  12. Re: know my ip

    Kenny McCormack wrote:
    > In article <076440ba-6bbf-4639-9009-4c42512e9275@y71g2000hsa.googlegroups.com>,
    > David Schwartz wrote:
    >> On Oct 16, 7:11 am, asit wrote:
    >>
    >>>> If I understand right, what you want is not you own actual IP but the
    >>>> IP everybody sees from the internet. For example my IP is 10.0.0.31
    >>>> and my gateway is 10.0.0.2 . But if I enterhttp://whatismyip.com/the
    >>>> page says: "Your IP Address Is 190.138.107.47" which is my public IP.
    >>>> Is this what you want to find out??
    >>> Yes...

    >> Presumably, you are going to tell the IP to someone. So why not just
    >> connect to them and say "my IP is whatever IP you see this connection
    >> coming from". This ensure that you give them the correct answer for
    >> them to reach you.
    >>

    >
    > There are a lot of practical ways that that can fail.
    >

    Such as?

    --
    Ian Collins

  13. Re: know my ip

    gazelle@shell.xmission.com (Kenny McCormack) writes:

    > In article ,
    > wrote:
    > ...
    >>lynx -dump checkip.dyndns.org
    >>
    >>But this will stopt working if www.whatismyip.com or
    >>checkip.dyndns.org disappear or change.

    >
    > Right. That's the crux of it. There doesn't seem to be anyway to do it
    > that doesn't depend on knowing the name of something external.


    Even if a reliable external source were available, access to it could
    be blocked or, worse, intercepted and the result altered.

    > Of course it then follows that the only thoroughly reliable ways to do
    > this is to do it yourself - i.e., have some machine of your own be on
    > the public net. One of the posters in the go2linux page had that
    > situation - where he could get what he needed directly from ifconfig,
    > because his machine was actually on the public net.
    >
    > P.S. Another way to go about this is to try to query the information
    > from your own router - it obviously knows the external IP address. The
    > trick is figuring out a good automated way to get it to divulge the
    > information.


    That would still fail if there are two (or more) levels of NAT.

    If you *really* must know a public IP address corresponding to the
    machine, you should probably be on the public Internet in the first
    place, and not behind a NAT gateway.

    --
    Måns Rullgård
    mans@mansr.com

  14. Re: know my ip

    Måns Rullgård wrote:
    > gazelle@shell.xmission.com (Kenny McCormack) writes:
    >>
    >> P.S. Another way to go about this is to try to query the information
    >> from your own router - it obviously knows the external IP address. The
    >> trick is figuring out a good automated way to get it to divulge the
    >> information.

    >
    > That would still fail if there are two (or more) levels of NAT.
    >
    > If you *really* must know a public IP address corresponding to the
    > machine, you should probably be on the public Internet in the first
    > place, and not behind a NAT gateway.
    >

    Or use a fixed IP.

    --
    Ian Collins

  15. Re: know my ip

    On Oct 18, 12:15 am, Ian Collins wrote:
    > Måns Rullgård wrote:
    > > gaze...@shell.xmission.com (Kenny McCormack) writes:

    >
    > >> P.S. Another way to go about this is to try to query the information
    > >> from your own router - it obviously knows the external IP address. The
    > >> trick is figuring out a good automated way to get it to divulge the
    > >> information.

    >
    > > That would still fail if there are two (or more) levels of NAT.

    >
    > > If you *really* must know a public IP address corresponding to the
    > > machine, you should probably be on the public Internet in the first
    > > place, and not behind a NAT gateway.

    >
    > Or use a fixed IP.
    >
    > --
    > Ian Collins


    Thanx everyone...finally my problem is solved...

  16. Re: know my ip

    On Oct 17, 6:58*am, gaze...@shell.xmission.com (Kenny McCormack)
    wrote:

    > >Presumably, you are going to tell the IP to someone. So why not just
    > >connect to them and say "my IP is whatever IP you see this connection
    > >coming from". This ensure that you give them the correct answer for
    > >them to reach you.


    > There are a lot of practical ways that that can fail.


    Actually, it's precisely because there is no way that can fail that
    you should use it. Note that they still may not be able to reach you,
    but it won't be because they don't have the right address.

    DS


+ Reply to Thread