JS获取本地IP、MAC地址

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_8224/article/details/82571573

1、通过ActiveX控件

  1. <HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
  2. <META http-equiv=Content-Type content="text/html; charset=gb2312">
  3. <SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)"for=foo>
  4. // document.forms[0].txtMACAddr.value=unescape(MACAddr);
  5. document.forms[0].txtIPAddr.value=unescape(IPAddr);
  6. alert(unescape(IPAddr));
  7. // document.forms[0].txtDNSName.value=unescape(sDNSName);
  8. //document.formbar.submit();
  9. </SCRIPT>
  10. <SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
  11. if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
  12. {
  13. //if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
  14. //MACAddr = objObject.MACAddress;
  15. if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
  16. IPAddr = objObject.IPAddress(0);
  17. //if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
  18. //sDNSName = objObject.DNSHostName;
  19. }
  20. </SCRIPT>
  21. <META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
  22. <BODY>
  23. <OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT>
  24. <OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>
  25. <SCRIPT language=JScript>
  26. var service = locator.ConnectServer();
  27. var MACAddr ;
  28. var IPAddr ;
  29. var DomainAddr;
  30. var sDNSName;
  31. service.Security_.ImpersonationLevel=3;
  32. service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
  33. </SCRIPT>
  34. <FORM id=formfoo name=formbar action=NICPost.asp method=post>
  35. <INPUT value=00:05:5D:0E:C7:FA name=txtMACAddr>
  36. <INPUT value=192.168.0.2 name=txtIPAddr>
  37. <INPUT value=typ name=txtDNSName>
  38. </FORM>
  39. </BODY>
  40. </HTML>

<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE> <META http-equiv=Content-Type content="text/html; charset=gb2312"> <SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo> // document.forms[0].txtMACAddr.value=unescape(MACAddr); document.forms[0].txtIPAddr.value=unescape(IPAddr); alert(unescape(IPAddr)); // document.forms[0].txtDNSName.value=unescape(sDNSName); //document.formbar.submit(); </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> <META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD> <BODY> <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> <FORM id=formfoo name=formbar action=NICPost.asp method=post> <INPUT value=00:05:5D:0E:C7:FA name=txtMACAddr> <INPUT value=192.168.0.2 name=txtIPAddr> <INPUT value=typ name=txtDNSName> </FORM> </BODY> </HTML>

 

 

2、通过调用DOS命令

  1. <html>
  2. <mce:scripttype="text/javascript"><!--
  3. getIP=function(){
  4. var fso = new ActiveXObject("Scripting.FileSystemObject");
  5. var wsh = new ActiveXObject("WScript.Shell");
  6. wsh.Run("command.com /c ipconfig /all > C://ip.txt");
  7. var f = fso.OpenTextFile("C://ip.txt");
  8. var s = f.ReadAll();
  9. f.Close();
  10. document.write(s);
  11. alert(s.match(/IP Address(/.)+/:((/d{1,3}/.){3}(/d{1,3}))/)[0]);
  12. }
  13. // --></mce:script>
  14. <body></body>
  15. </html>

 

 

源地址:在网上找了一下其他获取IP的方法
这个直接通过Js来执行ipconfig命令获取IP,但是安全性能是不是较低啊
http://topic.csdn.net/t/20041010/12/3440117.html

这个是通过ActiveX控件来获取的
http://blog.csdn.net/nwf5d/archive/2010/06/29/5702581.aspx

展开阅读全文

没有更多推荐了,返回首页