xargs 形成 ipinfo.io 一行获取信息

xargs formation with ipinfo.io to get info in one line

http://ipinfo.io 特定信息标签的入门示例,如下所示:

cat ips.txt | xargs -I% curl -s http://ipinfo.io/%/org | paste -d"," ips.txt -

Return:

8.8.8.8,AS15169 Google Inc.
8.8.4.4,AS15169 Google Inc.
1.2.3.4,AS15169 Google Inc.

我想要多条信息,所以我将示例更改为:

cat ips.txt | xargs -I% curl -s http://ipinfo.io/%/city http://ipinfo.io/%/region | paste -d"," ips.txt - > ip_info.txt

Return:

156.221.17.167,Punjab
,Dol pri Ljubljani

我如何格式化我的请求以适应同一行中用逗号分隔的所有与 IP 相关的信息,如下所示:

156.221.17.167,Punjab,Dol pri Ljubljani

如果您需要多条信息,最好在一个请求中获取所有信息,然后使用 jq 提取您想要的信息。这是 IP 的完整 JSON 输出(有关更多示例,请参见 http://ipinfo.io/developers):

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3860,-122.0838",
  "org": "AS15169 Google Inc.",
  "postal": "94035"
}

然后使用 jq 将 IP、城市和国家提取到 CSV 中:

$ curl -s ipinfo.io/8.8.8.8 | jq -r '[.ip, .city, .country] | @csv'
"8.8.8.8","Mountain View","US"

如果我们有一个包含一堆 IP 的文件,我们可以这样做:

$ cat ips.txt | xargs -I% curl -s http://ipinfo.io/%/json | jq -r '[.ip, .city, .country] | @csv'