Geolocate IP addresses using FreeGeoIP

Welcome back command-line ninjas, I wanted to take a small break from Trix’ with Linux and decided to write myself a tool. I noticed nothing was really out that would give you a comprehensive overview of an IP, which is awesome for Recon, and being totally 1337 :sunglasses:

This tool is written in ruby and utilises freegeoip.net, Google maps, and wttr.in. You can download the whole script here so that you don’t have to mess around with copy and pasting :stuck_out_tongue:

Keep in mind you will need Mechanize installed, to install this ruby gem, just run gem install mechanize and you should be good to go!

Heres the script:

#!/usr/bin/env ruby

require 'json'
require 'mechanize'
require 'rubygems'
require 'open-uri'

puts "Looking up #{ARGV[0]}"

agent = Mechanize.new
data = agent.get("http://freegeoip.net/json/#{ARGV[0]}").body

json = JSON.parse(data)
agent.user_agent = "curl/7.50.0"
weather = agent.get("http://wttr.in/#{json["city"]}").body.split("\n")
puts "Country Code: " + json["country_code"]
puts "Country: " + json["country_name"]
puts "Region Code: " + json["region_code"]
puts "Region Name: " + json["region_name"]
puts "City: " + json["city"]
puts "Zip Code: " + json["zip_code"]
puts "Time Zone: " + json["time_zone"]
puts "Metro Code: " + json["metro_code"].to_s
puts "Google Maps: https://www.google.com/maps?z=12&t=m&q=loc:#{json["latitude"]}+#{json["longitude"]}"  

puts "Weather right now: "
puts weather[1..6]
puts ""

Let me explain how this script works. When you run it, you supply it 1 argument, the IP address or hostname of the person/device you want to track. And it returns the estimated address, relevant Google Maps link, and current weather.

Hope you enjoyed this! I will be using this a lot :wink: Cheers!

pry0cc

12 Likes

I haven’t tried it out myself, but dude… it looks AWESOME!

1 Like

When I tried to use it, it hung pretty hard.

Seems pretty dang awesome mate! :smile:

1 Like

Oh yeah? Did it run at all? I’ve tested it with ruby 2.3.0

1 Like

Py3 rewrite, idea credit goes to @pry0cc

The only difference is that it displays full weather info (too lazy to crop).
You can download the script from pastebin.


--------------------------------------------------
longitude: -122.0574
latitude: 37.4192
region_code: CA
country_name: United States
country_code: US
city: Mountain View
metro_code: 807
ip: 216.58.212.142
region_name: California
time_zone: America/Los_Angeles
zip_code: 94043

Weather for City: Mountain View, United States of America

    \  /       Partly Cloudy 
  _ /"".-.     16 Β°C          
    \_(   ).   β†˜ 7 km/h       
    /(___(__)  16 km          
               0.0 mm         
                                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                       
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Wed 03. Aug β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Morning            β”‚             Noon      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    Evening            β”‚            Night             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    \  /       Partly Cloudy  β”‚     \   /     Sunny          β”‚     \   /     Sunny          β”‚     \   /     Clear          β”‚
β”‚  _ /"".-.     15 – 17 Β°C     β”‚      .-.      20 – 21 Β°C     β”‚      .-.      22 – 23 Β°C     β”‚      .-.      16 Β°C          β”‚
β”‚    \_(   ).   β†— 8 – 9 km/h   β”‚   ― (   ) ―   β†’ 10 – 12 km/h β”‚   ― (   ) ―   β†’ 21 km/h      β”‚   ― (   ) ―   β†— 10 – 11 km/h β”‚
β”‚    /(___(__)  10 km          β”‚      `-’      10 km          β”‚      `-’      10 km          β”‚      `-’      10 km          β”‚
β”‚               0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                       
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Thu 04. Aug β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Morning            β”‚             Noon      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    Evening            β”‚            Night             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    \  /       Partly Cloudy  β”‚     \   /     Sunny          β”‚     \   /     Sunny          β”‚               Overcast       β”‚
β”‚  _ /"".-.     12 – 15 Β°C     β”‚      .-.      18 – 19 Β°C     β”‚      .-.      24 – 25 Β°C     β”‚      .--.     19 Β°C          β”‚
β”‚    \_(   ).   β†— 9 – 10 km/h  β”‚   ― (   ) ―   β†— 12 – 13 km/h β”‚   ― (   ) ―   β†’ 18 – 21 km/h β”‚   .-(    ).   β†— 11 – 13 km/h β”‚
β”‚    /(___(__)  10 km          β”‚      `-’      10 km          β”‚      `-’      10 km          β”‚  (___.__)__)  10 km          β”‚
β”‚               0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚               0.0 mm | 0%    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                       
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ Fri 05. Aug β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Morning            β”‚             Noon      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    Evening            β”‚            Night             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     \   /     Sunny          β”‚     \   /     Sunny          β”‚     \   /     Sunny          β”‚     \   /     Clear          β”‚
β”‚      .-.      18 Β°C          β”‚      .-.      18 – 19 Β°C     β”‚      .-.      21 – 22 Β°C     β”‚      .-.      15 – 16 Β°C     β”‚
β”‚   ― (   ) ―   β†— 9 – 11 km/h  β”‚   ― (   ) ―   β†— 6 – 14 km/h  β”‚   ― (   ) ―   β†’ 19 km/h      β”‚   ― (   ) ―   β†— 8 – 12 km/h  β”‚
β”‚      `-’      10 km          β”‚      `-’      10 km          β”‚      `-’      10 km          β”‚      `-’      10 km          β”‚
β”‚     /   \     0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚     /   \     0.0 mm | 0%    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

https://www.google.com/maps?z=12&t=m&q=loc:@37.4192,-122.0574
--------------------------------------------------

#!/usr/bin/python3

import requests
import argparse
from html.parser import HTMLParser


class DirtyWttrParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.start = False
        self.stop = False
        self.weather = str()

    def is_doable(self):
        return self.start + self.stop

    def handle_starttag(self, tag, attrs):
        if tag == 'pre':
            self.start = True

    def handle_endtag(self, tag):
        if tag == 'pre':
            self.stop = True

    def handle_data(self, data):
        if self.is_doable() == 1:
            self.weather += data


def get_data(url, type):
    try:
        r = requests.get(url)
    except Exception as e:
        print("[-] Failed fetching data")
        print(e)
        return
    if type == 'json':
        return r.json()
    elif type == 'html':
        return r.text


def main():
    # parse script args (IP)
    argsp = argparse.ArgumentParser()
    argsp.add_argument("ip", help="IP to check", type=str)
    args = argsp.parse_args()

    # init components
    ip_to_check = args.ip
    url_geoip = 'http://freegeoip.net/json/'
    url_wttr = 'http://wttr.in/'
    url_maps = 'https://www.google.com/maps?z=12&t=m&q=loc:@'
    parser = DirtyWttrParser()

    # get info
    ip_info = get_data(url_geoip + ip_to_check, 'json')
    parser.feed(get_data(url_wttr + '@' + ip_to_check, 'html'))

    # display info
    print('-' * 50)

    for k, v in ip_info.items():
        print('{}: {}'.format(k, v))

    print(parser.weather)

    print('{}{},{}'.format(url_maps, ip_info['latitude'], ip_info['longitude']))

    print('-' * 50)

if __name__ == '__main__':
    main()
4 Likes

Awesome! You know cropping is easy.

Do a split on newline, and then just print array item 0-6.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.