Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So we have an old company intranet that we are waiting to move to Sharepoint. In the meantime, I wanted to make it easier for our regular staff seeking MIS/IT Support to click on a link on the intranet page to show their local computer hostname. We have one that already pops up when clicked on and shows their IP address, but I prefer their hostname. The current page is written with mixed javascript and ASP.net stuff. I am not much of a coder, but I can see and understand some codes to be able to make small edits. I've done a lot of research and just can't get the page to act the same as the current "GetIP.asp" page does. Any help would be greatly appreciated.

Here is current GetIP.asp:

<!-- metadata type="typelib" file="c:\windows\system32\scrrun.dll" -->
<html>
<head>

<%
Dim UserIPAddress
UserIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserIPAddress = "" Then
  UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
End If
%>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Computer IP Address</title>
<script language="JavaScript">
<!--
// This will resize the window when it is opened or
// refresh/reload is clicked to a width and height of 500 x 500
// with is placed first, height is placed second
window.resizeTo(500,150)
window.moveTo(350,300)
-->
</script>

<style type="text/css">

</style>

</head>
<body>
<center>
<h1 style="font-size:60";"face:Verdana">
<%
	Response.Write( UserIPAddress )
%>

</h1></center>
</body>
</html>


What I have tried:

This is the GetHostname.asp that I have tried to make work to show hostname but doesnt work:

<!-- metadata type="typelib" file="c:\windows\system32\scrrun.dll" -->
<html>
<head>

<%
System.Net.IPHostEntry host;
host = System.Net.Dns.GetHostEntry(HttpContext.Current.Re quest.ServerVariable["REMOTE_HOST"]);
string strComputerName = host.HostName;
%>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>WKS/LTP Name</title>
<script language="JavaScript">
<!--
// This will resize the window when it is opened or
// refresh/reload is clicked to a width and height of 500 x 500
// with is placed first, height is placed second
window.resizeTo(500,150)
window.moveTo(350,300)
-->
</script>

<style type="text/css">

</style>

</head>
<body>
<center>
<h1 style="font-size:60";"face:Verdana">
<%
	Response.Write("WKS/LTP Name is: " + strComputerName); 
%>

</h1></center>
</body>
</html>
Posted
Updated 16-Nov-22 3:42am

1 solution

An .asp file is an old-school "classic" ASP page. It runs VBScript, which is an ancient language with very few built-in tools.

The code in your GetHostname.asp file is C#; that will only work in an ASP.NET page.

You could try renaming your file to GetHostname.aspx and replacing the first line (<!-- metadata ... -->) with:
ASPX
<%@ Page Language="C#" %>
Whether that works will depend on whether or not your server has the .NET Framework installed, and the IIS site has "managed code" enabled. If you're on a server that's as old as the code, that may not be the case.

Edit: The corrected C# code would look something like:
ASPX
<%@ Page Language="C#" %>
<%
string hostName = Request.UserHostName;
if (string.IsNullOrEmpty(hostName))
{
    string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(ipAddress)) ipAddress = Request.UserHostAddress;
    
    try
    {
        System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry(ipAddress);
        hostName = entry.HostName;
    }
    catch 
    {
        hostName = ipAddress;
    }
}
%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>WKS/LTP Name</title>
    <script>
    window.resizeTo(500, 150);
    window.moveTo(350, 300);
    </script>
    
    <style>
    h1 {
        font-size: 60rem;
        font-family: Verdana;
    }
    </style>
</head>
<body>
    <h1>
        WKS/LTP Name is:
        <%= HttpUtility.HtmlEncode(hostName) %>
    </h1>
</body>
</html>
NB: You need to HTML-encode the host name, just in case it contains any "special" characters.
 
Share this answer
 
v2
Comments
Drew Creamer 16-Nov-22 10:02am    
Thanks for the info. I am about to try now. The server currently is running Windows 2012 R2 DC, IIS 6 and .NET 3.5 and 4.5 is installed.
Drew Creamer 16-Nov-22 10:07am    
So I took the full first line out that has (<--metadata ... -->) and replaced with the ASPX line you suggested. and renamed the file to GetHostname.aspx

I was getting a 500 server error window that popped up. Now I get a 404 - File or directory not found. I need to find out if I have managed code enabled. Do you know how to find out if that's enabled or not and how to enable?
Drew Creamer 16-Nov-22 10:30am    
Another thought too. Can a menu.asp that's embedded to a .html file call on this newer .aspx file without issue? lol Sorry to sound dumb. just not sure of all the coding stuff. The current site has been pieced together by multiple people who are no longer with the company. The menu.asp is what has the code below to call on the aspx file. i am pasting some of the code from that menu.asp below:

<ul id="submenu1" style="display:none;">
	  	<li id="submenu2"><div class="divSubMenu1" onMouseOver="changeOverColor me" onMouseOut="changeOutColor me" onClick="open('/DeptPages/HelpDesk.html'),'main','post'" title="MIS Help Desk">MIS Ticket System</div>
       <li id="submenu2"><div class="divSubMenu1" onMouseOver="changeOverColor me" onMouseOut="changeOutColor me" onClick="open('/DeptPages/GetHostname.aspx'),'new','post'" title="Find WKS/LTP Name">What's my WKS/LTP Name?</div>
		<li id="submenu2"><div class="divSubMenu1" onMouseOver="changeOverColor me" onMouseOut="changeOutColor me" onClick="open('/DeptPages/GetIP.asp'),'new','post'" title="Find PC IP Address">What is my IP?</div></ul>
Richard Deeming 16-Nov-22 10:32am    
That depends on how you're trying to "call" it. If you're just linking to it, then it will work. If you're trying to include it within an .asp page, it won't work; you'd need to use an <iframe> to show it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900