在页面获取本地电脑IP

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    ResourceBundle resource = ResourceBundle.getBundle("config");
    String ctx = request.getContextPath();
    request.setAttribute("ctx", ctx);
%>
<!-- <html lang="en" style="min-height: 100%;overflow: hidden">
<head lang="zh-cn">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <meta content="MSHTML 6.00.2800.1106" name="GENERATOR">
</head>
<body> -->

    <!--<form id="formfoo" name="formbar" action="#" method="post" style="display:none">
        <input value="00:05:5D:0E:C7:FA" name="txtMACAddr">
        <input value="192.168.30.68" name="txtIPAddr">
        <input value="lzf" name="txtDNSName">
    </form>-->
<!-- </body>
</html> -->

<!--[if !IE]> -->
<!-- <![endif]-->

<script type="text/javascript">

$(function () {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1;
    if (userAgent.indexOf("Chrome") > -1){
        gooleGetIP();
        return "Chrome";
    }
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
        ieGetIP();
        return "IE";
    };
});
function gooleGetIP(){
    function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
          //compatibility for firefox and chrome
          var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
          var pc = new myPeerConnection({
             iceServers: []
         }),
         noop = function() {},
         localIPs = {},
         ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
         key;

         function iterateIP(ip) {
             if (!localIPs[ip]) onNewIP(ip);
             localIPs[ip] = true;
        }

          //create a bogus data channel
         pc.createDataChannel("");

         // create offer and set local description
         pc.createOffer().then(function(sdp) {
             sdp.sdp.split(‘\n‘).forEach(function(line) {
                 if (line.indexOf(‘candidate‘) < 0) return;
                 line.match(ipRegex).forEach(iterateIP);
             });

             pc.setLocalDescription(sdp, noop, noop);
         });

         //sten for candidate events
         pc.onicecandidate = function(ice) {
             if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
             ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
         };
    }

    // Usage

    getUserIP(function(ip){
        //alert("Got IP! :" + ip);
        $.ajax({
            url:"${ctx}/system/saveUserLoginIP",
            data:{loginIp:ip},
            type:"post",
            dataType:"json",
            async:false, //同步
            success:function(data){
                console.log(data.data);
            },
            error:function(data){
                alert(data);
            }
        });
    });
};

function ieGetIP() {

}
</script>

<object id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></object>
<object id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></object>

<script language="JScript">
    var service = locator.ConnectServer();
    var MACAddr;
    var IPAddr;
    var DomainAddr;
    var sDNSName;
    service.Security_.ImpersonationLevel = 3;
    service.InstancesOfAsync(foo, ‘Win32_NetworkAdapterConfiguration‘);
</script>
<script language="JScript" event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for="foo">
   /*  document.forms[0].txtMACAddr.value = unescape(MACAddr);
    document.all.txtIPAddr.value = unescape(IPAddr);
    document.forms[0].txtDNSName.value = unescape(sDNSName); */
    //alert(unescape(IPAddr));
    $.ajax({
        url:"${ctx}/system/saveUserLoginIP",
        data:{loginIp:unescape(IPAddr)},
        type:"post",
        dataType:"json",
        async:false, //同步
        success:function(data){
            console.log(data.data);
        },
        error:function(data){
            alert(data);
        }
    });
</script>
<script language="JScript" event="OnObjectReady(objObject,objAsyncContext)" for="foo">
    if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true) {
        if(objObject.MACAddress != null && objObject.MACAddress != "undefined") {
            MACAddr = objObject.MACAddress;
        }
        if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") {
            IPAddr = objObject.IPAddress(0);
        }
        if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined") {
            sDNSName = objObject.DNSHostName;
        }
    }
</script>

原文地址:https://www.cnblogs.com/lijianda/p/11212419.html

时间: 2024-10-13 10:43:16

在页面获取本地电脑IP的相关文章

关于Java获取本地服务器IP的问题

Java获取本地服务器IP,我们最简单的写法: import java.net.InetAddress; public class CatchIp{ public static void main(String[] args) { try { // 获取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("IP地址:"+ip); } catch (Exception e) {

2014-11-10 关于C# 如何获取本地电脑的UTC时区

  首先庆祝一下 我竟然在这C#公司做了一年了还没被炒 然后在吐槽一下万恶的需求 本来今天的是要获取UTC时区的 花了1小时的时间去百度搜索,现在就来分享一下 结果吧!发现C# 本身有一个System.TimeZoneInfo 该属性 当然也是通过微软的文档看到的,通过定义了一个List<TimeZoneInfo>(这里可能会打错了) 来存储,因为这里我要获取的是一个集合.好了这样就获取我们电脑里的UTC了 当然这时候 我将获取到的List拿来出来 因为我需要定义一个枚举来存储这些时区,我通过

获取本地公网IP地址的方法

适用场景: 服务器地址为net映射地址,本机ifconfig无法直接获取映射的公网地址. 方法1: [[email protected]1 nidongde]# curl http://ifconfig.me 50.1xx.2xx.18 ifconfig.me的地址为日本地址,访问可能比较慢. 方法2: [[email protected]1 nidongde]# curl http://www.net.cn/static/customercare/yourip.asp 2>/dev/null|

获取本地连接ip地址(通用版)

@echo off & setlocal enabledelayedexpansionrem 如果系统中有route命令,优先采用方案1:for /f "tokens=3,4" %%a in ('route print^|find "0.0.0.0 0.0.0.0"') do ( set "gate=%%a" & set "ip=%%b")for /f "tokens=2" %%a in (

获取本地主机ip

Windows平台: char* getlocalhostip () { char *ip=NULL; WORD wVersionRequested; WSADATA wsaData; char name[255]; PHOSTENT hostinfo; wVersionRequested = MAKEWORD(2,0); if(WSAStartup(wVersionRequested, &wsaData) == 0) { if(gethostname(name, sizeof(name)) =

window系统上用PHP获取本地物理IP代码

<?php $res=system("ipconfig -all"); $contents=trim(ob_get_clean()); $contents=str_replace("\r\n","",$contents); $contents=explode(" ",$contents); foreach($contents as $k=>&$v) { $pre=substr($v,0,8); if($pr

java 获取本地电脑的分辨率代码

1.代码: java.awt.Toolkit tk=java.awt.Toolkit.getDefaultToolkit();       java.awt.Dimension screenSize=tk.getScreenSize(); int screenWidth=screenSize.width;              //System.out.println(screenWidth);//1440       int screenHeight=screenSize.height; 

【C#】C#获取本地的内网(局域网)和外网(公网)IP地址的方法

1.获取本机的IP地址集合: 1 /// <summary> 2 /// 获取本机所有ip地址 3 /// </summary> 4 /// <param name="netType">"InterNetwork":ipv4地址,"InterNetworkV6":ipv6地址</param> 5 /// <returns>ip地址集合</returns> 6 public s

获取本地IP方法

获取本地服务器IP经常会出现127.0.0.1,0:0:0:0:0:0:0:1,fe80:0:0:0:960:74bd:e1a0:e5b9%11 这些情况,以下代码可解决此问题 public static void main(String[] args) { try { Enumeration<NetworkInterface> enumeration = NetworkInterface .getNetworkInterfaces(); while (enumeration.hasMoreE