JS Five ways to get IP, Mac, and host names _javascript tips

Source: Internet
Author: User
Today in the JS (JavaScript) to obtain the client IP applet, the Internet search, many in the current system and browsers are ineffective, very helpless, in Chrome, Firefox rarely get the direct use of ActiveX access to IP and other JS script. The following code is what I tested on all windowsNT5.0 and above systems, giving the code:

Method one (for IE only and the client's IE allows Acitivex to run, via platform: xp,server03,2000):
Gets the client IP.
Copy Code code as follows:

<HTML>
<HEAD>
<TITLE>GetLocalIP</TITLE>
</HEAD>
<BODY>
Get IP:
<script language= "JavaScript" > Function getlocalipaddr () {var osetting = null; var IP = null; try{osetting = new Ac Tivexobject ("Rcbdyctl.") Setting "); ip = osetting.getipaddress; if (Ip.length = = 0) {return "is not connected to the Internet";} osetting = null; }catch (e) {return IP;} return IP; } document.write (Getlocalipaddr () + "<br/>") </script>
</BODY>
</HTML>

Method Two (all platforms and browsers):
Get the IP of the client in the network, provided the client is networked.
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>javascript get client ip[use Sina interface]</title>
<body>
<script type= "Text/javascript" src= "http://counter.sina.com.cn/ip/" charset= "gb2312" ></script> <!-- Get interface data, pay attention to CharSet-->
<script type= "Text/javascript" >
Document.writeln ("IP address:" +ildata[0]+ "<br/>"); IP address in output interface data
Document.writeln ("Address type:" +ildata[1]+ "<br/>"); The type of IP address in the output interface data
Document.writeln ("Address type:" +ildata[2]+ "<br/>"); The provinces and cities of the IP address in the output interface data
Document.writeln ("Address type:" +ildata[3]+ "<br/>"); Of the IP address in the output interface data.
Document.writeln ("Address type:" +ildata[4]+ "<br/>"); Operator of IP address in output interface data
</script>
</body>

Method Three (ie only and the client's IE allows Acitivex to run):
Call the VBS script to get the computer name (some people do not know what the computer name is, the simple explanation is that it is the physical name of the machine rather than the user name you are using) and the user name of the login.
Copy Code code as follows:

<HTML>
<HEAD>
<TITLE>WMI Scripting Html</title>
</HEAD>
<BODY>
<script language=javascript>
var WshShell =new ActiveXObject ("Wscript.Shell");
document.write ("Computer name =" + wshshell.expandenvironmentstrings ("%computername%") + "<br/>");
document.write ("Login username =" + wshshell.expandenvironmentstrings ("%USERNAME%") + "<br/>");
</script>
</BODY>
</HTML>

Method Four (ie only and the client's IE allows Acitivex to run):
Get the name of the computer, the username and domain name of the login (if you joined the domain, show which domain your machine is in).
Copy Code code as follows:

<HTML>
<HEAD>
<TITLE>WMI Scripting Html</title>
</HEAD>
<BODY>
<script language=javascript>
var wshnetwork = new ActiveXObject ("Wscript.Network");
document.write ("Domain name =" + wshnetwork.userdomain+ "<br/>");
document.write ("Computer name =" + wshnetwork.computername+ "<br/>");
document.write ("Login username =" + wshnetwork.username+ "<br/>");
</script>
</BODY>
</HTML>

Method Five (ie is only allowed and the client's IE allows Acitivex to run):
Be able to obtain LAN IP address, local Mac, and machine name (code source network).
Copy Code code as follows:

<title></title>
<body>
<object classid= "Clsid:76a64158-cb41-11d1-8b02-00600806d9b6" id= "locator" style= "display:none;visibility": Hidden "></object>
<object classid= "clsid:75718c9a-f029-11d1-a1ac-00c04fb6c223" id= "foo" style= "Display:none;visibility:hidden" ></object>
<form name= "MyForm" >
<br/>mac Address: <input type= "text" name= "macAddress" >
<br/>ip Address: <input type= "text" Name= "IPAddress" >
<br/> Host Name: <input type= "text" name= "HostName" >
</form>
</body>
<script language= "JavaScript" >
var smacaddr= "";
var sipaddr= "";
var sdnsname= "";
var service = Locator. ConnectServer ();
Service. Security_. impersonationlevel=3;
Service. Instancesofasync (foo, ' Win32_NetworkAdapterConfiguration ');
</script>
<script for= "foo" event= "OnObjectReady (objobject,objasynccontext)" language= "JScript" >
if (objobject.ipenabled!= null && objobject.ipenabled!= "undefined" && objobject.ipenabled = = True) {
if (objobject.ipenabled && objobject.ipaddress (0)!=null && objobject.ipaddress (0)!= "undefined")
sipaddr = objobject.ipaddress (0);
if (objobject.macaddress!= null &&objobject.macaddress!= "undefined")
SMACADDR = objobject.macaddress;
if (objobject.dnshostname!= null &&objobject.dnshostname!= "undefined")
Sdnsname = Objobject.dnshostname;
}
</script>

<script for= "foo" event= "oncompleted (Hresult,perrorobject, Pasynccontext)" language= "JScript" >
MYFORM.MACADDRESS.VALUE=SMACADDR;
MYFORM.IPADDRESS.VALUE=SIPADDR;
Myform.hostname.value=sdnsname;
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.