name: ip-lookup description: 检查一个IP地址在多个公共地理位置和声誉源,并返回最佳匹配的位置摘要。
IP查询技能
目的
- 查询多个公共IP信息提供商并聚合结果,为IP地址生成简洁的最佳匹配位置和元数据摘要。
功能
- 查询至少四个公共源(例如 ipinfo.io, ip-api.com, ipstack, geoip-db, db-ip, ipgeolocation.io)或其免费端点。
- 规范化返回数据(国家、地区、城市、经纬度、组织/ASN)并计算简单匹配分数。
- 返回一个紧凑摘要,包含最佳匹配源和其他源的简短表格。
注意事项
- 公共API可能有速率限制或需要API密钥以支持高流量;技能尽可能回退到免费端点。
- 地理位置是近似的;ISP/网关位置可能与最终用户位置不同。
Bash示例(使用curl + jq):
# 基本用法:IP作为第一个参数传递
IP=${1:-8.8.8.8}
# 查询4个源
A=$(curl -s "https://ipinfo.io/${IP}/json")
B=$(curl -s "http://ip-api.com/json/${IP}?fields=status,country,regionName,city,lat,lon,org,query")
C=$(curl -s "https://geolocation-db.com/json/${IP}&position=true")
D=$(curl -s "https://api.db-ip.com/v2/free/${IP}" )
# 输出最佳匹配的启发式方法应在脚本中实现
echo "单行摘要:"
jq -n '{ip:env.IP,sourceA:A,sourceB:B,sourceC:C,sourceD:D}' --argjson A "$A" --argjson B "$B" --argjson C "$C" --argjson D "$D"
Node.js示例(推荐):
// ip_lookup.js
async function fetchJson(url, timeout = 8000){
const controller = new AbortController();
const id = setTimeout(()=>controller.abort(), timeout);
try { const res = await fetch(url, {signal: controller.signal}); clearTimeout(id); if(!res.ok) throw new Error(res.statusText); return await res.json(); } catch(e){ clearTimeout(id); throw e; }
}
async function ipLookup(ip){
const sources = {
ipinfo: `https://ipinfo.io/${ip}/json`,
ipapi: `http://ip-api.com/json/${ip}?fields=status,country,regionName,city,lat,lon,org,query`,
geodb: `https://geolocation-db.com/json/${ip}&position=true`,
dbip: `https://api.db-ip.com/v2/free/${ip}`
};
const results = {};
for(const [k,u] of Object.entries(sources)){
try{ results[k] = await fetchJson(u); } catch(e){ results[k] = {error: e.message}; }
}
// 规范化并选择最佳匹配(基于国家+城市的简单多数)
const votes = {};
for(const r of Object.values(results)){
if(!r || r.error) continue;
const country = r.country || r.country_name || r.countryCode || null;
const city = r.city || r.city_name || null;
const key = `${country||'?'}/${city||'?'}`;
votes[key] = (votes[key]||0)+1;
}
const best = Object.entries(votes).sort((a,b)=>b[1]-a[1])[0];
return {best: best?best[0]:null,score: best?best[1]:0,results};
}
// 用法:node ip_lookup.js 8.8.8.8
代理提示
“使用ip-lookup技能查询至少四个公共IP信息提供商以获取{ip}。返回一个简短的JSON摘要:best_match(国家/城市)、score以及每个源的详细信息(国家、地区、城市、经度、纬度、组织)。尊重速率限制并在错误时回退到替代端点。”
“创建新技能时,遵循SKILL_TEMPLATE.md格式并包括Node.js和Bash示例。”