Get country using the system geoip command.

Started by ., Jul 10, 2015, 07:14 PM

Previous topic - Next topic

.

This is a dummy function to capture the output from the command line version of the geoiplookup command. (tested on Debian)

function GetCountry(ip)
{
    ::system(::format(@"geoiplookup %s > .tmp", ip));
    local s = "", p, f;
    if (f = ::file(".tmp", "r")) {
        local b = f.readblob(f.len());
        foreach (c in b) {
            s += ::format(@"%c", c);
        }
        f.close();
    }
    remove(".tmp");
    p = (p = s.find(":")) ? ++p : 0;
    return ::strip(s.slice(p));
}

Example:
local v = GetCountry("80.60.233.195");
print(v);

Output:
NL, Netherlands
I haven't tested the command any further than that but you are free to report any bugs you encounter. It's not the best approach but it's a fun way to do it that doesn't need manual update and leaving the OS manage the database updates (which could be really old in some cases).

NOTE: The command could be named differently based on your OS.
NOTE: The command could have a different output based on your OS.
NOTE: You might need to install the command manually based on your OS.
NOTE: The server needs to have write permissions in the working folder. In order to create the temporary file for the command output.
NOTE: The code could be easily updated to include a custom database file. In case the OS has a really old one.

Be sure to adjust the code for your needs in any of those cases.

Example getting the geoip command on debian:
sudo apt-get install geoip-bin geoip-database
.