bash脚本 获取磁盘大小_如何从Bash脚本获取系统的地理位置

bash脚本 获取磁盘大小

bash脚本 获取磁盘大小

World map with internet connections concept
Toria/Shutterstock.com Toria / Shutterstock.com

You can find the geographic location of a remote Linux system using open APIs and a simple bash script. Geolocating a server could help you track it in the physical world, ensuring servers are located in regional hotspots.

您可以使用开放的API和简单的bash脚本找到远程Linux系统的地理位置。 对服务器进行地理定位可以帮助您在物理世界中进行跟踪,从而确保服务器位于区域热点中。

Each server on the internet has a public-facing IP address. This is either directly assigned to the server, or assigned to a router that sends traffic to that server. IP addresses give us a clue about where that server is located in the world. We can get this geolocation data through two open APIs, provided by ipinfo.co and IP Vigilante and use it to see the city, state, and country associated with a server or other remote system. This doesn’t give you a precise GPS location; it just lets you see the general area of the IP address.

互联网上的每个服务器都有一个面向公众的IP地址 。 它要么直接分配给服务器,要么分配给向该服务器发送流量的路由器。 IP地址为我们提供了有关该服务器在世界上的位置的线索。 我们可以通过ipinfo.co和IP Vigilante提供的两个开放的API获取此地理位置数据,并使用它来查看与服务器或其他远程系统关联的城市,州和国家。 这无法为您提供精确的GPS位置; 它只是让您看到IP地址的常规区域。

连接到远程系统 (Connect to a Remote System)

You’ll be running the following commands on the Linux server or other remote systems you want to geolocate, so you must connect to the server and access a shell on it first. For example, you might connect via SSH. You could run the commands on your local system to find its location, but you probably already know where you are!

您将在要定位Linux服务器或其他远程系统上运行以下命令,因此必须连接到该服务器并首先访问该服务器上的Shell。 例如,您可以通过SSH连接 。 您可以在本地系统上运行命令以找到其位置,但是您可能已经知道您的位置!

安装curl和jq (Install curl and jq)

We need two tools to access the geolocation API: curl to make HTTP requests and  jq to process the JSON data that we get back. Open a terminal and use  apt-get to install these tools on Ubuntu or Debian-based systems. On other Linux distributions, use your Linux distribution’s package installation tool instead.

我们需要两种工具来访问地理定位API: curl进行HTTP请求, jq处理返回的JSON数据。 打开一个终端,并使用apt-get在基于Ubuntu或Debian的系统上安装这些工具。 在其他Linux发行版上,请改用Linux发行版的软件包安装工具。

sudo apt-get install curl jq

查找服务器的公共IP地址 (Find the Server’s Public IP Address)

We also need the server’s public IP address before we can get the geolocation data. Use curl to make an API call to ipinfo.io in your terminal window.

在获取地理位置数据之前,我们还需要服务器的公共IP地址。 使用curl在您的终端窗口中对ipinfo.io进行API调用。

curl https://ipinfo.io/ip

从API获取位置数据 (Get Location Data From The API)

Now that we have the public IP of the server, we can make a call to ipvigilante.com’s API to get the geolocation data. Replace <your ip address> with the address that came back in the previous command.

现在我们有了服务器的公共IP,我们可以调用ipvigilante.com的API来获取地理位置数据。 将<your ip address>替换为上一条命令中返回的地址。

curl https://ipvigilante.com/<your ip address>
output from curl command

Let’s take a closer look at what data we get back from this call:

让我们仔细研究一下从此调用中获取的数据:

metadata showing location information

The API returns the city, country, and continent, in which our server resides. It also returns the approximate latitude and longitude coordinates, in case we want to draw this server on an interactive map. We’ll be using “latitude,” “longitude,” “city_name,” and “country_name” in our script. The  jq command understands how to process the API data and extract these four fields out.

该API返回我们服务器所在的城市,国家和大洲。 如果我们想在交互式地图上绘制此服务器,它还会返回近似的纬度和经度坐标。 我们将在脚本中使用“纬度”,“经度”,“ city_name”和“ country_name”。 jq命令了解如何处理API数据并提取出这四个字段。

创建脚本以自动执行API调用 (Creating a Script to Automate The API Call)

We can create a script that grabs the geolocation data and writes it to a file in CSV format. The data will be written to a file called server_location.txt in the /tmp/ directory. Open your favorite editor and create a script named geolocate.sh . Insert the script contents shown below, and be sure to replace the IP address with your own:

我们可以创建一个脚本,以获取地理位置数据并将其写入CSV格式的文件中。 数据将被写入/tmp/目录中名为server_location.txt的文件中。 打开您喜欢的编辑器,然后创建一个名为geolocate.sh的脚本。 插入如下所示的脚本内容,并确保将IP地址替换为您自己的IP地址:

#!/bin/sh

OUTPUT_FILE=/tmp/server_location.txt

# Grab this server's public IP address
PUBLIC_IP=`curl -s https://ipinfo.io/ip`

# Call the geolocation API and capture the output
curl -s https://ipvigilante.com/${PUBLIC_IP} | \
        jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name' | \
        while read -r LATITUDE; do
                read -r LONGITUDE
                read -r CITY
                read -r COUNTRY
                echo "${LATITUDE},${LONGITUDE},${CITY},${COUNTRY}" | \
                        tr --delete \" > \
                        ${OUTPUT_FILE}
        done

Save the script and go back to the terminal. Make the script executable from the terminal, by granting the execute permission on this file.

保存脚本并返回到终端。 通过授予对此文件的执行权限,使脚本可从终端执行。

chmod u+x geolocate.sh

Now you’re ready to test it out. Run the geolocate.sh script and check the contents of the output file:

现在您可以进行测试了。 运行geolocate.sh脚本并检查输出文件的内容:

./geolocate.sh
cat /tmp/server_location.txt

running the geolocate script

通过Cron作业每天更新一次地理位置数据 (Updating the Geolocation Data Once a Day With a Cron Job)

Let’s create a cron job to make our server update its geolocation and save it to a file once a day. The daily cron job updates a file called server_location.txt in the /tmp/ folder of the server. Creating a 24-hour cron job is as easy as putting our script into the /etc/cron.daily directory. We must use the sudo command to copy the file as the root user, to avoid permission issues. Run the following command to copy geolocate.sh to the /etc/cron.daily directory.

让我们创建一个cron作业,以使我们的服务器每天更新其地理位置并将其保存到文件中。 日常cron作业会更新server_location.txt /tmp/文件夹中名为server_location.txt的文件。 创建24小时的cron作业就像将脚本放入/etc/cron.daily目录一样容易。 我们必须使用sudo命令以root用户身份复制文件,以避免权限问题。 运行以下命令,将geolocate.sh复制到/etc/cron.daily目录。

sudo cp geolocate.sh /etc/cron.daily

These changes are immediate, and our script will run every 24 hours to update the contents of the /tmp/server_location.txt file. We can use this data to do interesting things, such as plotting our servers on a map as well as combining geolocation with traffic logs to see where in the world our server hotspots are.

这些更改是立即的,我们的脚本将每24小时运行一次,以更新/tmp/server_location.txt文件的内容。 我们可以使用这些数据来做一些有趣的事情,例如在地图上绘制服务器以及将地理位置与流量日志结合起来以查看服务器热点在世界上的哪个位置。

翻译自: https://www.howtogeek.com/405088/how-to-get-your-systems-geographic-location-from-a-bash-script/

bash脚本 获取磁盘大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值