Create an IP Address geolocation data search virtual server with a visual map

Contributed by: Arnold Batista

Description

The following irule will create a IP address Geolocation Data Search Virtual Server that will output the results with a google map of the IP location along with IP, Country, State, ISP, and Org. This is more convenient than ssh’ing into a gtm to do a geoip_lookup.

iRule Source

when HTTP_REQUEST {
    set ip [URI::query [HTTP::uri] ip]
    set response "<html>\n  <head>\n    <title>IP Address Geolocation Data Search</title>\n  </head>\n  <body>\n"

    if { ![catch {IP::addr $ip mask 255.255.255.255} ] } {
        set geo_properties { country state city zip isp org }

        append response "    <div align=\"center\">
      <h2>IP Address Geolocation Data Search</h2>
      <table cellpadding=\"3\" cellspacing=\"0\" border=\"1\">
        <tr>
          <td><strong>IP</strong></td>
          <td>$ip</td>
        </tr>\n"

        foreach property $geo_properties {
            set result [whereis $ip $property]

            if { $result ne "" } {
                append response "        <tr>
          <td><strong>[string toupper $property]</strong></td>
          <td>[string toupper $result]</td>
        </tr>\n"
            }
        }

        append response "      </table>\n"

        set latitude [whereis $ip latitude]
        set longitude [whereis $ip longitude]


        if { ($latitude != 0) && ($longitude != 0) } {
            append response "      <br />\n      <iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" style=\"border-width: 2px; border-color: black; border-style: solid;\" src=\"http://maps.google.com/maps?q=$latitude,$longitude&hl=en&ie=UTF8&t=m&z=14&iwloc=A&ll=$latitude,$longitude&output=embed\"></iframe><br /><small><a href=\"http://maps.google.com/maps?q=37.771008,$longitude&hl=en&ie=UTF8&t=m&z=14&iwloc=A&ll=$latitude,$longitude&source=embed\" style=\"color:#0000FF;text-align:left\">View Larger Map</a></small>\n      <br />\n      <h3>lat: $latitude, long: $longitude</h3>\n"
        } else {
            append response "      <br />\n      <iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" style=\"border-width: 2px; border-color: black; border-style: solid;\" src=\"http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=[whereis $ip city],+[whereis $ip abbrev]&aq=0&oq=Cus&z=5&iwloc=A&output=embed\"></iframe><br /><small><a href=\"http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=[whereis $ip city],+[whereis $ip abbrev]&iwloc=A\" style=\"color:#0000FF;text-align:left\">View Larger Map</a></small>\n"
        }

        append response "    </div>"
    } else {
        append response {    <div align="center">
      <h2>IP Address Geolocation Data Search</h2>
      <form>
        <strong>IP:</strong> <input type="text" name="ip" />
        <input type="submit" value="Search" />
      </form>
    </div>}
    }

    append response "\n  </body>\n</html>"

    HTTP::respond 200 content $response
}

The BIG-IP API Reference documentation contains community-contributed content. F5 does not monitor or control community code contributions. We make no guarantees or warranties regarding the available code, and it may contain errors, defects, bugs, inaccuracies, or security vulnerabilities. Your access to and use of any code available in the BIG-IP API reference guides is solely at your own risk.