✅脚本如下 中文输出到文件保存
#!/bin/bashoutput_file="ip_result.txt"
ips="ipfile.txt"
> "$output_file" while read ip; doecho "正在查询:$ip"result=$(curl -s "http://ip-api.com/json/$ip?lang=zh-CN")ip_address=$(echo "$result" | jq -r '.query')country=$(echo "$result" | jq -r '.country')region=$(echo "$result" | jq -r '.regionName')city=$(echo "$result" | jq -r '.city')isp=$(echo "$result" | jq -r '.isp')status=$(echo "$result" | jq -r '.status')if [[ "$status" == "success" ]]; thenline="$ip_address 来自 $country $region $city,运营商:$isp"echo "$line"echo "$line" >> "$output_file"elseecho "$ip 查询失败"echo "$ip 查询失败" >> "$output_file"fisleep 2
done < "$ips"
📌 输出示例
正在查询:104.23.221.74
104.23.221.74 来自 美国 弗吉尼亚州 阿什本,运营商:Cloudflare, Inc.
⚙️ 使用方法:
- 保存脚本为
get_ip_cn.sh
- 给它执行权限:
chmod +x get_ip_cn.sh
- 运行脚本:
./get_ip_cn.sh
- 查看结果输出文件:
cat ip_result.txt