It seems like there is a package called geoclue which uses Mozilla's location service to lookup wireless access points and their known location, but there doesn't seem to be a command line interface for this. Is there another way to do it? Any clever hacks?
- 174,089
- 51
- 332
- 407
- 395
6 Answers
If you want IP based lookup... (but you'll have to fix the HTML formatting of the output)
wget http://cqcounter.com/whois/my_ip_address.php && egrep "IP Location|City|Latitude|Longitude" my_ip_address.php
- 2,103
Here's my own best answer, which tries out all of the different geoclue providers available in Ubuntu:
apt-get install geoclue geoclue-ubuntu-geoip python-geoclue geoclue-yahoo geoclue-plazes geoclue-localnet geoclue-gypsy
echo "#!/usr/bin/env python
import Geoclue
providers = 'Gypsy, Hostip, Localnet, Plazes, Skyhook, Yahoo, Ubuntu GeoIP'
providers = providers.split(', ')
for provider in providers:
POS_PROVIDER = provider
location = Geoclue.DiscoverLocation()
location.init()
location.set_position_provider(POS_PROVIDER)
position = location.get_location_info()
print provider
print position['latitude']
print position['longitude']
" > location.py
python location.py
This results in:
Gypsy
0.0
0.0
Hostip
39.8121
-76.9837
Localnet
39.8121
-76.9837
org.freedesktop.DBus.GLib.UnmappedError.GeoclueErrorQuark.Code1: Could not understand reply from server
Plazes
39.8121
-76.9837
org.freedesktop.DBus.GLib.UnmappedError.GeoclueErrorQuark.Code1: Couldn't parse response from web service
Skyhook
39.8121
-76.9837
Yahoo
39.8121
-76.9837
Ubuntu GeoIP
40.6501
-73.9496
Which is a big step forward, but it seems that all of the providers are using IP based lookup, and my VPN totally throws that off. Seems like there should be a provider that only uses wifi access points, but I haven't found it yet.
- 395
I use curl http://ip-api.com
If you want to filter:
- Current country and city:
curl http://ip-api.com/json?fields=country,city - Current coordinates:
curl http://ip-api.com/json?fields=lat,lon
- 151
- 4
To get your location by IP you can use:
curl -s http://whatismycountry.com/ | sed -n 's|.*> *\(.*\)</h3>|\1|p'
To get your coordinates by IP you can use:
curl -s http://whatismycountry.com/ | sed -n 's/.*Coordinates \(.*\)<.*/\1/p'
- 174,089
- 51
- 332
- 407
Here's a Python 2.x script similar to the one posted by @Mike McKay, which will use all the available Geoclue providers and output their location information in a pretty-printed format (requires apt-get install python-geoclue):
#!/usr/bin/env python
from __future__ import print_function
import Geoclue
from datetime import datetime
print("Geoclue version %s" % Geoclue.VERSION)
dl = Geoclue.DiscoverLocation()
dl.init()
providers = dl.get_available_providers()
for provider in providers:
pname = provider['name']
dl.set_position_provider(pname)
position = dl.get_location_info()
print("\n%s\n%s" % (pname, "-"*len(pname)))
for k, v in position.items():
if k.endswith('_timestamp'):
v = datetime.fromtimestamp(v)
print("%-25s%s" % (k+':',v))
- 671
There is a workaround that use your IP and find your location based to your Real IP.
first install lnyx
sudo apt-get install lynx-cur
Now the command is:
lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\'
- 87,123