importClass("java.net.InetAddress");
importClass("java.net.NetworkInterface");
importClass("java.net.Inet6Address");
let intranetIP = getIntranetIP();
let publicData = getPublicData();
let data = util.format("所在城市: %s\n内网ip: %s\n外网ip: %s", publicData.cityName, intranetIP, publicData.ip);
log(data);
alert(data);
function getIntranetIP() {
// 获取内网IP地址
let networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
let networkInterface = networkInterfaces.nextElement();
let inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
let inetAddress = inetAddresses.nextElement();
if (inetAddress instanceof Inet6Address) {
continue;
}
let ip = inetAddress.getHostAddress();
log(ip);
if (!"127.0.0.1".equals(ip)) {
return inetAddress.getHostAddress();
}
}
}
}
function getPublicData() {
//获取公网ip地址
let r = http.get("http://pv.sohu.com/cityjson?ie=utf-8");
let data = r.body.string();
eval(data);
return {
ip: returnCitySN.cip,
cityName: returnCitySN.cname,
};
}