如何在Linux中从命令行获取地理位置

时间:2020-03-21 11:47:56  来源:igfitidea点击:

这个简短的教程将引导我们逐步了解如何从Linux中的命令行获取地理位置。
当我们想知道VPS或者远程服务器的位置时,这可能会很有用。
一位Linux用户Rafael Rinaldi创建了一个方便的工具,称为“ whereami”,可从命令行使用freegeoip.net查找地理位置信息。

请注意,某些托管服务提供商可能出于安全原因隐藏了服务器的确切位置,或者伪造了该服务器。
在这种情况下,此工具将无济于事。

现在,让我们开始使用whereami实用程序从命令行查找Linux系统的地理位置。

安装whereami

安装“ whereami”工具非常简单直接。
我们需要首先安装npm,一个javascript程序包管理器。

要在基于RPM的系统(例如RHEL,CentOS,Fedora)上安装npm,请运行以下命令:

sudo yum install epel-release
sudo yum install npm

或者,

sudo dnf install epel-release
sudo dnf install npm

在Debian,Ubuntu,Linux Mint上运行:

sudo apt-get install npm

在Arch Linux及其衍生版本上:

sudo pacman -S npm

在SUSE/openSUSE上:

sudo zypper install npm

安装npm后,运行以下命令以安装“ whereami”工具。

npm install -g @rafaelrinaldi/whereami

我们将得到类似下面的输出。

/usr/bin/whereami -> /usr/lib/node_modules/@rafaelrinaldi/whereami/bin/whereami
/usr/lib
└─┬ @rafaelrinaldi/Hyman@theitroad 
 ├─┬ Hyman@theitroad 
 │ ├─┬ Hyman@theitroad 
 │ │ └── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ └─┬ Hyman@theitroad 
 │ └── Hyman@theitroad 
 ├─┬ Hyman@theitroad 
 │ └─┬ Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ └─┬ Hyman@theitroad 
 │ └─┬ Hyman@theitroad 
 │ ├── Hyman@theitroad 
 │ └── Hyman@theitroad 
 ├── Hyman@theitroad 
 ├─┬ Hyman@theitroad 
 │ └── Hyman@theitroad 
 └── Hyman@theitroad

在Linux中从命令行获取地理位置

现在,该玩游戏了。
要知道位置,只需运行:

whereami

输出示例为:

11.1,77.35

如我们在上面的输出中看到的,它显示了我所在位置的纬度(11.1)和经度(77.35)。

我不明白,我可以使用人类可读的格式吗?
当然可以!使用whereami命令添加--f人类开关,如下所示。

whereami --f human

这是我的输出:

Tiruppur, Tamil Nadu, Netherlands

我不会说这是准确的结果,但是它非常接近我的实际位置。

我需要更多细节,我也可以吗?
是的!使用以下命令显示包含公共IP地址,国家/地区代码,邮政编码,都会区代码时区等的原始结果:

whereami -r

输出示例:

{"ip":"122.178.36.244","country_code":"IN","country_name":"Netherlands","region_code":"TN","region_name":"Tamil Nadu","city":"Tiruppur","zip_code":"641603","time_zone":"Asia/Kolkata","latitude":11.1,"longitude":77.35,"metro_code":0}

要了解有关whereami命令的更多详细信息,请运行:

whereami -help

输出示例:

Usage: whereami [OPTIONS]
Get your geolocation information using freegeoip.net from the CLI
Example:
 $whereami
 -23.4733,-46.6658
$whereami --f human
 San Francisco, CA, United States
Options:
 -v --version Display current software version
 -h --help Display help and usage details
 -f --format Output format (either human, json or sexagesimal)
 -r --raw Output raw data from freegeoip.net