Thread: Extract IP from HTML of webpage

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    7

    Extract IP from HTML of webpage

    Hello, I am just beginning on sockets in C and I need to make a program that finds my external IP.

    Through a bit of research on this forum I decided to make the program request the source of www.whatsmyip.com and find the IP.

    I have got the source:

    Code:
    char* file_contents = GetInternetFile(TEXT("http://www.whatsmyipaddress.com/"), 100000, &sz);
    But please can you tell me how I would extract the IP after the text "your ip is".

    Would I be using scanf?

    Or is there a more direct way of doing this?

    Cheers

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Well assuming GetInternalFile stores all the html in the file_contents buffer you could use strstr() to find the occurence of your ip is, and take the ip address after that.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    Cheers for quick reply.

    Ok so I get all contents starting at "Your IP Address is":

    printf(strstr(file_contents, "Your IP Address is"));

    Now what function would I use to put the IP address into a variable.

    And what parameters could I put on the strstr() function to make it start at the ip.

    I am thinking maybe if it starts with the ip i can read the 1st 12 characters.

    Thanks

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No. First, check your URLs - Do you mean http://whatismyip.com, or the one listed in your code?

    Second, due to the nature of these types of sites, most of them have policies about how a script (or even if a script should) hit their site for IP addresses. Some outright ban it. Some limit it. Keep in mind that these people might pay by the bandwidth, so don't abuse it!

    http://whatismyip.com seems to be a bit of a gem - a very easygoing policy, of the ones I've seen. On their homepage, there is a large (huge) tab called "Automation". You should read this page. Then, you should not go after the main page, but the page that the automation page points you to, namely, here. This is much nicer, in terms of bandwidth, and it's easier for you as a programmer, as it has only the data you need.

    The other website you list in your code, http://www.whatsmyipaddress.com, doesn't seem to list a policy. If you decide to hit this site, don't pound them with requests.

    Shop around, too. Other sites might offer more or less data, under easier or harder terms. If you have web space, making a script to report IP is usually trivial.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    Thats exactly what I was looking for. Cheers

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    strstr finds the first occurrence of the requested string in the provided string, and returns a pointer to the beginning of it.

    Now, you need to set the pointer to where the actual string you want is. You could then copy out your data by walking the string until you hit a character that's not a number or a period (dot).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
       // Want to get the needle
       const char * const haystack = "This is a string with a needle in it.";
       const char * const search = "a string with a";
       const char *p = strstr(haystack, search);
       if (p)
       {
          // p now points to "a string with a"
          // Increment p past the end of the needle
          p += strlen(search);
    
          while (*p && isspace(*p)) ++p; // skip any spaces
    
          if (*p == '\0')
             return 1;
    
          const char *start_of_needle = p;
    
          while (*p && !isspace(*p)) ++p; // find the next space
    
          char *needle = malloc(p - start_of_needle + 1);
          if (!needle)
             return 1;
          strncpy(needle, start_of_needle, p - start_of_needle);
          needle[p - start_of_needle] = '\0';
    
          printf("Needle: %s\n", needle);
          free(needle);
       }
    
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    I am only beginning C but that still helped. Useful bit of code.

    Okay so used the automation page/directory in the latter ip website.

    As I have a separate .c file for finding the ip I need to pass it to the file that actually uses the ip in a format that can be used like this - to construct a socket:

    Obviously needs changing a bit.

    Code:
        memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
        echoServAddr.sin_family = AF_INET;                /* Internet address family */
        echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
        echoServAddr.sin_port = htons(echoServPort);      /* Local port */
    Problem is I get a compile warning - and dont shoot me if this is a lame question, im new to c:
    get_ip.c [Warning] return makes integer from pointer without a cast

    Code:
    char get_ip(void)
    {
    	size_t sz;
    	
        char* file_contents = GetInternetFile(TEXT("http://www.whatismyip.com/automation/n09230945.asp"), 100, &sz);
    
    	return file_contents;
    }

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Should be:
    Code:
    char* get_ip(void)

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    That did the trick.

    My last question now is, how would I get the internal IP address of my machine?

    Can I have a few hints on how to use the:

    gethostbyname() on the value gethostname() returns.

    method please.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    Dont worry about last question - have researched more and found the answer.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just have your own web server running on a machine built just to collect IPs and extract the IPs from your log files? What exactly are you trying to do, John?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Library which extract html tags content
    By Bargi in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2007, 10:17 PM
  3. html form - writing ip to a hidden field
    By iain in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-16-2001, 03:29 AM