JavaScript – Getting environmental variables and IP address

Retrieve logged on user, MAC address, IP, computer name, etc.

JavaScript:

// Get computername, IP, MAC, etc
	function computername()
	{
	var wshShell = new ActiveXObject("WScript.Shell");
	var computername = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%");
	return computername
	}

	function username()
	{
	var wshshell=new ActiveXObject("wscript.shell");
	var username=wshshell.ExpandEnvironmentStrings("%username%");
	return username
	}

	function ipAddress() {
		var ipAddress = "";
		var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
		e = new Enumerator(wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
			for(; !e.atEnd(); e.moveNext()) {
				var s = e.item(); 
				ipAddress = s.IPAddress(0);
			}
		return ipAddress
		
	}

	function macAddress() {
		var macAddress = "";
		var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
		e = new Enumerator(wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
			for(; !e.atEnd(); e.moveNext()) {
				var s = e.item(); 
				macAddress = s.MACAddress;
			}
		return macAddress		
	}
	
	/* Global variables */
	var cname = computername();
	var uname = username();
	var ip = ipAddress();
	var mac = macAddress();

HTML:

<script type="text/javascript"> document.write(cname);</script><br>
<script type="text/javascript"> document.write(ip);</script><br>
<script type="text/javascript"> document.write(mac);</script>

Leave a Reply

Your email address will not be published. Required fields are marked *