特定信息标记的http://ipinfo.io入门示例如下:
cat ips.txt | xargs -I% curl -s http://ipinfo.io/%/org | paste -d"," ips.txt -
返回:
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
返回:
156.221.17.167,Punjab
,Dol pri Ljubljani
我如何格式化我的请求以使所有与IP相关的信息在同一行中用逗号分隔','如下所示:
156.221.17.167,Punjab,Dol pri Ljubljani
答案 0 :(得分:1)
如果您想要多条信息,最好在一个请求中完成所有信息,然后使用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'