http://qs321.pair.com?node_id=11118411

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

use Geo::IP; my $gi = Geo::IP->open("/somedirectory/dat/GeoIP.dat"); my $record = $gi->record_by_addr('103.2.106.33'); print $record->country_code, $record->country_code3, $record->country_name, $record->region, $record->region_name, $record->city, $record->postal_code, $record->latitude, $record->longitude, $record->time_zone, $record->area_code, $record->continent_code, $record->metro_code;

The output I got from the above (based on the code at https://metacpan.org/pod/Geo::IP) is:

-180.0000-180.00000--0

I was expecting to see a country name in the print outputs...

Am I missing something here?

Also, the GeoIP.dat file is no longer available at Maxmind) so I'm using a previous version. How or where do I get the new dat format?

Replies are listed 'Best First'.
Re: Help with Geo::IP output
by Tux (Canon) on Jun 24, 2020 at 11:40 UTC

    As my fellow monk already mentioned, MaxMind can be done using App::geoip:

    $ geoip -w 103.2.106.33 GeoIP data for 103.2.106.33 - (hostname not found): CIDR : 103.2.104.0/22 IP range : 103.2.104.0 - 103.2.107.255 Provider : Singapore Telecommunications Limited City : Country : SG Singapore Continent : Asia Timezone : Asia/Singapore Location : 1.3667 / 103.8000 (50) 1°22'00.12" / 103°48'0 +0.00" https://www.openstreetmap.org/#map=13/1.3667/103.8000 https://www.google.com/maps/place/@1.3667,103.8000,13z Location : 52.2892 / 5.2603 52°17'21.13" / 5°15'3 +6.92" Distance : ± 10467.30km Whois information: Name : Singapore Telecommunications Limited ID : CO122-AP Phone : +65 83180832 EMail : opsinfosvc@singtel.com Abuse : opsinfosvc@singtel.com Address : 7 Bedok South Road NCS Bedok EU member : No Satellite : No Anon Proxy: No

    Enjoy, Have FUN! H.Merijn

      Thanks you for much for the example.

      How do you call it from your script after you have installed the module?

        Currently you don't. It is a module in the App:: namespace. If you want to use the infrastructure generated by geoip steal the code from geoip or use it's json output in a pipe:

        use JSON::MaybeXS; open my $gh, "-|", "geoip", "--json", $url_or_hostname or die; my $data = decode_json (do { local $/; <$gh> }); say $data->[0]{provider}; # [0] as it returns a list of matches

        Enjoy, Have FUN! H.Merijn

      I got the following error:

      FATAL: no pg_hba.conf entry for host "[local]", user "xxx", database +"geoip", SSL off Cannot continue without a working database
      How do I set up the config file?

        Use a supported database, see dsn.

Re: Help with Geo::IP output
by marto (Cardinal) on Jun 24, 2020 at 10:19 UTC

    "Also, the GeoIP.dat file is no longer available at Maxmind) so I'm using a previous version. How or where do I get the new dat format?"

    See GeoIP revisited, App::geoip.

Re: Help with Geo::IP output
by afoken (Chancellor) on Jun 29, 2020 at 23:38 UTC

    Just a minor note on guessing locations from IP addresses:

    It does not work, period.

    I'm currently sitting in Hamburg, Germany. Google can't even guess the right federal state from my public IP address. It guesses that I'm in Norderstedt, a pimple on Hamburg's butt small city close to Hamburg, but in the federal state of Schleswig-Holstein. (Hamburg is both a city and a federal state.) Why? Because my internet provider is actually two tightly coupled internet providers, one working in Hamburg as a private company, and another one working in Norderstedt as a private-public partnership company. (Yes, that sounds insane. But it works quite well.) Both share large parts of their infrastructure, and because the borderline between Hamburg and Schleswig-Holstein is literally just a few hundred meters away from my home, my internet line probably crosses the borderline and connects to network infrastructure located in Norderstedt instead of going a longer way to network infrastructure in Hamburg. Yes, it is an edge case, but still, even for this most trivial case, IP geolocation does not work.

    Now, due to the COVID-19 pandemic, I worked from home for several weeks, using a VPN to my employer's office. All outgoing traffic is tunnelled through the VPN by policy, and despite sitting in Hamburg, that stupid geolocation guesswork now thinks that I'm 40 km away from Hamburg, close to Lübeck. Guessing that location is no big problem, because it has a fixed IP address, unlike my home.

    Years ago, I had the same situation working on a VPN, but for a company that had its main office in Munich, Bavaria. And the geolocation guesswork had a "slight" error of about 600 km (800 km by car) at guessing my location. Imagine how wrong the guessed location is for their empoyees working on location all over the world, connected through the VPN to the Munich office. IP-based geolocation won't even guess the right continent!

    Now imagine a pizza delivery service checking my "location" from my public IP address. "Yo, dude, are you kiddin' me? You're sittin' in frickin' Bavaria, we won't drive 800 km twice to deliver a pizza!" Luckily, all pizza delivery services I use give a sh*t about that geolocation nonsense. They simply deliver to the address that I type into their web form. And if they think there's something fishy, they simply call your phone number.

    Streaming services and some other retards do actually try to guess my location to restrict access. Well, use a server in the proper location, set up a VPN, issue solved.

    Some clowns also try to guess where all of those evil hackers originate from, that trigger their snake oil incoming ping alert scam, or bring down their home-grown web frame work by issueing two requests withing one second. "And they all come from Russia, no China, no, wait, who's the evil state of the hour? Antarctia! Yes, that's where all of those evil people come from to invade our home county by hacking our computers. Just look at them! All of those little people dressed in black and white, with big *NAKED* yellow feet and those ridiculously large yellow noses. You really can't trust them!"

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      I agree that geographical location is informative only, but the registrar data is definitive. If I have an intrusion detection system for a voting system in the US, and you see repeated intrusion attempts from an IP that is registered in Russia, it is highly likely *not* a local registered voter. (swap any of the two countries with random other two countries)

      You don't need an exact location for that.

      And yes, that is one of the experiments I am doing. Nothing is automatic (yet), but reports are quite informative this way: I have my blocking and detection systems report the IP's to an analyser that expands them with Geographical location and registrar data.

      Fun facts: North Korea has only one registered CIDR of 1024 IP's. The Vatican has 321 CIDR's with a total of 13056 IP's.


      Enjoy, Have FUN! H.Merijn
        The Vatican has 321 CIDR's with a total of 13056 IP's.

        And only 453 residents. Mysterious ways, huh?

      Thanks for the reminder. For my purpose, I just need to know the country of the IP - not the specific state or city.

      I am able to use geoip from a perl script from the command line but I can't get the same script (with the shebang line added and correct cgi permission of 755 set) to run as a web script. It's frustrating (my apologies to those who helped with answering my questions from my earlier post venting my frustration).

      I saw someone suggesting Geo::IPfree and tried it. It works so I'm using that now.

Re: Help with Geo::IP output
by $h4X4_&#124;=73}{ (Monk) on Jun 26, 2020 at 18:51 UTC
    I use Geo::IPfree. when you go to the software77 website to download the database pick "Geo::IPfree format" .

    Usage;
    my $host = $ENV{REMOTE_ADDR} || $ENV{REMOTE_HOST} || ''; # Module use Geo::IPfree; # Make object with database path my $geo = Geo::IPfree->new('/IpToCountry.dat'); # Memoize for speed $geo->Faster; # Get country name my $country_name = ($geo->LookUp( $host ))[1];

      The big disadvantage of Geo::IPfree is that it only returns the country.


      Enjoy, Have FUN! H.Merijn
Re: Help with Geo::IP output
by Anonymous Monk on Jun 24, 2020 at 15:21 UTC

    Quite surprised there's no easy way. I couldn't use App::geoip because of the error below:

    FATAL:  no pg_hba.conf entry for host "[local]", user "xxx", database "geoip", SSL off

    While I have MySQL, I have never used PostgreSQL and I'm not familiar with it.

      There is an easy way, use SQLite, see DATABASE linked from from dsn which I linked previously. You need to read the documentation for the tool.

      Update: typo, dns/dsn. Thanks Your Mother.

        The App::geoip page doesn't explain how to use it with SQLite. Ir only says it works with SQLite.

Re: Help with Geo::IP output
by Anonymous Monk on Jun 29, 2020 at 10:29 UTC

    So I'm trying to run the geoip command from a script. Here's the code:

    my @output = qx(geoip -s 209.197.123.153); print Dumper(\@output); print $output[5]; #Country : US United States

    The qx command seems to do what I'm trying to accomplish. Am I doing it correctly? I'm getting the information I need, which is the country info.

    Someone posted the code below the other day. I didn't get it to work:

    use JSON::PP qw/decode_json/; use IPC::Run3::Shell::CLIWrapper; my $geoip = IPC::Run3::Shell::CLIWrapper->new( { fail_on_stderr=>1, st +dout_filter => sub {$_=decode_json($_)} }, qw/ geoip --json --DB=dbi: +SQLite:dbname=geoip / ); my $data = $geoip->('115.66.224.70');
Re: Help with Geo::IP output
by Anonymous Monk on Jun 25, 2020 at 06:54 UTC

    I upgraded my server's SQLite and I'm finally able to create a save a database. Using the ls, I the database file "geoip.db" is created after the .save command.

    What is the next step for me?

Re: Help with Geo::IP output
by Anonymous Monk on Jun 29, 2020 at 15:13 UTC

    This module should be thrown away! There are so many things wrong with it. If you try use it have a web script call it, you run into problems of permission and whatnot!

      This comment won't help anyone. If you'd be more specific, there might be someone who can tel you what to do to get whatever you want to work.

      Besides that, this complaint does not tell wich module (in your perception) is crap (many were named in this discussion). Maybe it was never designed to be called from a webpage, maybe the author never considered it being called from a webpage, maybe the call from a webpage might be illegal. Too many possible reasons for failing permissions.

      Care to try again and tell us which module fails how and what you would like to achieve?

      FWIW, it is oke for you to decide to uninstall it, but comments like this will not cause it to be thrown away. If however you found behavior that causes illegal actions, we will have to consider taking it out of CPAN.


      Enjoy, Have FUN! H.Merijn