Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Client Computer name with either Classic ASP or Client Side VBScri 2

Status
Not open for further replies.

ironhide1975

Programmer
Feb 25, 2003
451
US
Is there any easy way to grab a computer name with ASP Classic or Client Side VBScript?

Any help is appreciated.

 
Client-side.
[tt]
set wshnetwork=createobject("wscript.network")
scomputername=wshnetwork.computername
set wshnetwork=nothing
[/tt]
 
like this?
Code:
<script language="vbscript">
set wshnetwork=createobject("wscript.network")
scomputername=wshnetwork.computername
set wshnetwork=nothing
</script>

 
That's what I meant. Security zone setting should be low enough at most prompting the user for consent to let it execute.
 
A co-worker also gave me this, but both scripts do not seem to give me the Computer name. Any ideas?
Code:
<%
                    Dim sIP
                    Dim oShell, oExec, sCommand, sOutput
                    sIP = Request.ServerVariables("REMOTE_ADDR")
                    ''watch for line wrap - begin
                    sCommand = "%comspec% /c [at] echo off & for /f ""tokens=2"" %q in ('ping -n1 -a " & sIP & "^|find /i ""pinging""') do echo %q"
                    ''watch for line wrap - end
                    Set oShell = CreateObject("WScript.Shell")
                    Set oExec = oShell.Exec(sCommand)
                    sOutput = oExec.StdOut.ReadAll
                    Set oExec = Nothing
                    Set oShell = Nothing
                    scomputername = sOutput
                    %>

 
ironhide1975,
ping -a 0.0.0.0 will return machine name. what you have is close... the following should help - untested togeather (though i have used all of the code in seperate batch jobs):

test.bat
Code:
REM script to return all ip's used in a subnet
REM Just change the base IP address after from 255.255.255.%2 to whatever range you want (keep the %2 though).

@echo off

if %1!==! goto start
if %1==h goto hundreds
if %1==t goto tens
if %1==u goto units
:start
echo Starting ping 
echo Starting ping > pinglist.txt
for %%h in (0,1,2) do call %0 h %2%%h
goto end
:hundreds
for %%t in (0,1,2,3,4,5) do call %0 t %2%%t
goto end
:tens
for %%u in (0,1,2,3,4,5,6,7,8,9) do call %0 u %2%%u
goto end
:units
ping -a -n 1 192.168.5.%2|find "Pinging" >> pinglist.txt
IF %2!==259! goto repeat
:end

:: gets only 2nd element of pinglist 
:repeat
for /f "eol=; tokens=2 delims= " %%i in (pinglist.txt) do @echo %%i>>list.txt
if "%i%"=="" goto repeat1
goto repeat
:ender

:: strips everything after 1st instance of '.' or ' '
:repeat1
for /f "eol=; tokens=1 delims=. " %%i in (list.txt) do @echo %%i>>lista.txt
if "%i%"=="" goto repeat2
goto repeat1
:ender1

:: gets rid of 192 and ping
:repeat2
awk "{if($1!=192) print $1}" lista.txt >>listb.txt
this will take awhile to run. you may want to break it out in sections. you will also need awk, but yuo can google for it - free.
regards,
longhair
 
yes - i know this is the vbscript forum, but seeing how it works in dos might inspire some ideas and i believe you can call a bat job with vbscript. correct?
regards,
longhair
 
ironhide, you said:
>A co-worker also gave me this, but both scripts do not seem to give me the Computer name
which I suppose including the lines I posted?

Here are my comments:

[1] For my suggestion, in case you did not capture the main idea of it, is that work on a client machine via a html script. It can be run soon after the page is loaded via a onload handler. The scomputername is a variable and you must store it in a form, maybe hidden field, which will return to the server in the next round trip after "submitting". If you have no idea what I am talking about, ask you co-worker first or post back.

[2] For you second script, I doubt it _very_ much that it will work.

[2.1] sCommand
>[tt]sCommand = "%comspec% /c [at] echo off & for /f ""tokens=2"" %q in ('ping -n1 -a " & sIP & "^|find /i ""pinging""') do echo %q"[/tt]

What is the [at]? Maybe the forum trnasformed it from "@"? Even that, when you ping a site with its ip address, the echo will not contain the machine name (even within the domain). It is not the case if you ping the machine name, then ip address may be revealed in the echo back.

[2.2] Besides the syntax will not suite the wshshell.exec method and will hang the server. You _must_ take out the %comspec% /c part.

[2.3] An alternative is to capture the stdout.readall into a string variable, and device a regexp of pattern "Pinging machinename [ipaddress]" (figurative only). _But_, since you ping the ipaddress, the resulting pattern will only be "Ping ipaddress" in the stdout. That's what I mean in [2.1].

If you know the ipaddress through the server variable as you've shown, then you can use a little free asp component doing a reverse lookup of your domain. You can refer to a previous thread I'd posted:
Check it out.

- tsuji
 
Further note:

An immediate note to further a point I'd overlooked: it is that you use -a switch. It does take away the inconvenience I mentioned in [2.1]/[2.3]. The result may be not completely reliable, but it is viable.

In that case, the procedure is this.
[1] Take away the %comspec% /c in the scommand.
[2] Use simply this scommand:
[tt]scommand="ping -n1 -a " & sIP[/tt]
[3] Capture stdout.readall to a string variable, sout, say.
[4] Device the regexp

[tt]set regex=new regexp
with regex
.ignorecase=true
.global=false
.pattern="Pinging.*?\[" & sIP & "\]"
end with
set matches=regex.execute(sout)
scomputername=matches.item(0)
scomputername=replace(replace(scomputername,"Pinging ","")," [" & sIP & "]",""))
[/tt]
Then you might get a sensible scomputername. You might try it first. Detail might need some fine tuning. (The regexp pattern I just write off hand. It might need some refinement. But, I think it is about that.)

- tsuji
 
tsuji,
nice detailed explanations. a star for you.
regards,
longhair
 
I don't do a lot of web development, so I'm not sure if any of these methods will work for you but it looks like they may be a little easier if they do.

Using Local System Environment Variables:
Code:
set shell = WScript.CreateObject( "WScript.Shell" )
computername = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
wscript.echo computername

Using Hostname
Code:
SET objShell=CreateObject("Wscript.Shell")
DIM objExec, strResult
SET objExec = objShell.Exec("hostname")
strResult = objExec.StdOut.ReadAll
WScript.Echo strResult

Using WMI
Code:
Set wmiColl = GetObject("WinMgmts:root/cimv2")_
  .ExecQuery("Select * FROM Win32_ComputerSystem")
For Each wmiObj in wmiColl
  WScript.Echo wmiObj.Name
Next

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top