我在下面有以下Shell脚本,可以将网站下载到一个变量中。据我所知。我想做的是,当我执行带有参数(IP地址)的Shell脚本时,从控制台将输入添加到该网站(接受IP地址,并输出一个位置),以便它可以输出该网站的地理位置。 IP地址。任何人都可以帮忙。
#! /bin/bash
read input
content=$(wget http://freegeoip.net -q )
echo $content
将以下内容另存为一个文件getgeo
:
#!/bin/bash
location=$(curl -s http://freegeoip.net/csv/$1)
echo $location
然后像这样使用它:
chmod +x getgeo
./getgeo 141.20.1.33
"141.20.1.33","DE","Germany","16","Berlin","Berlin","","52.5167","13.4000","",""
或者,如果只希望第5和第3个字段且不带引号,请执行以下操作:
./getgeo 141.20.1.33 | tr -d '"' | awk -F, '{print $5,$3}'
Berlin Germany
或者,您可以在脚本本身内部进行修整:
#!/bin/bash
location=$(curl -s http://freegeoip.net/csv/$1)
echo $location | tr -d '"' | awk -F, '{print $5,$3}'
如果您喜欢解析XML或JSON,则可以将更/csv/
改为/XML/
或/JSON/
,您将获得以下信息:
<?xml version="1.0" encoding="UTF-8"?> <Response> <Ip>92.238.99.46</Ip> <CountryCode>GB</CountryCode> <CountryName>United Kingdom</CountryName> <RegionCode>E6</RegionCode> <RegionName>Gloucestershire</RegionName> <City>Gloucester</City> <ZipCode>GL3</ZipCode> <Latitude>51.8456</Latitude> <Longitude>-2.1575</Longitude> <MetroCode></MetroCode> <AreaCode></AreaCode> </Response>
或JSON
{"ip":"141.20.1.33","country_code":"DE","country_name":"Germany","region_code":"16","region_name":"Berlin","city":"Berlin","zipcode":"","latitude":52.5167,"longitude":13.4,"metro_code":"","areacode":""}
笔记:
该命令tr -d '"'
从其作为输入接收的内容中删除所有双引号:
该-F,
交换机awk
到用途说逗号作为字段分隔符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句