<!--页面定义一个隐藏域-->
<input type="hidden" name="docip" id="docip" value=""></input>
<script>
//判断浏览器
if (myBrowser() == "IE") {
var docip = GetAdapterInfo()
document.getElementById('docip').value = docip
}
else {
notIeGetIp()
}
//获取浏览器信息
function myBrowser() {
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
var isIE = ((userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) || userAgent.indexOf(".NET") > -1) && !isOpera; //判断是否IE浏览器
var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
var isSafari = userAgent.indexOf("Safari") > -1
&& userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
var isChrome = userAgent.indexOf("Chrome") > -1
&& userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
if (isIE) {
return "IE";
}
if (isOpera) {
return "Opera";
}
if (isEdge) {
return "Edge";
}
if (isFF) {
return "FF";
}
if (isSafari) {
return "Safari";
}
if (isChrome) {
return "Chrome";
}
}
//IE获取
function GetAdapterInfo() {
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.ConnectServer("."); //连接本机服务器
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");
//查询使用SQL标准
var e = new Enumerator(properties);
var msg = "";
for (; !e.atEnd(); e.moveNext()) {
var p = e.item();
msg += p.IPAddress(0) + "|"
}
return msg.split('|')[0];
}
//Chrome内核浏览器获取
function notIeGetIp() {
var RTCPeerConnection = /*window.RTCPeerConnection ||62616964757a686964616fe4b893e5b19e31333365663465*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
var rtc = new RTCPeerConnection({ iceServers: [] });
if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed
rtc.createDataChannel('', { reliable: false });
};
rtc.onicecandidate = function (evt) {
// convert the candidate to SDP so we can run it through our general parser
// see https://twitter.com/lancestout/status/525796175425720320 for details
if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
};
rtc.createOffer(function (offerDesc) {
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
}, function (e) { console.warn("offer failed", e); });
var addrs = Object.create(null);
addrs["0.0.0.0"] = false;
function updateDisplay(newAddr) {
if (newAddr in addrs) return;
else addrs[newAddr] = true;
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
document.getElementById('docip').value = displayAddrs.join(" or perhaps ") || "n/a";
}
function grepSDP(sdp) {
var hosts = [];
sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
var parts = line.split(' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
})(); else {
document.getElementById('docip').innerHTML = "<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";
document.getElementById('docip').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";
}
}
</script>
相关链接
https://www.jianshu.com/p/8d7348bc84f4
https://zhidao.baidu.com/question/13759768.html