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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Host Discovery 1

Status
Not open for further replies.

twooly

MIS
Feb 22, 2004
122
US
I am trying to write a script that will discovery hosts on a subnet but I am running into one problem. For some reason the line in red doesn't output anything to the logfile. But if I put the actual run statment into a command prompt it outputs fine.

Anyone have some ideas on why not?

Code:
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2

vCleanedLog = "C:\SortedIP.Log"
vSubnet = "10.30.3."

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\IP.Log"

'Ping all the IPs in our subnet and write results to text file
For x = 1 To 25
	[COLOR=red]oShell.Run "%comspec% /c ping -a " & vSubnet & x & " >> " & sTempFile, 0, True[/color]
	WScript.Sleep 5000
Next

'now read the text file and strip out the junk
vReport = ""
MyList= oFSO.OpenTextFile(sTempFile, ForReading).ReadAll
myArray=Split(MyList,vbCrLf, -1, vbtextcompare)

For Each Val In myArray
    If Left(Val,7)= "Pinging" Then
        Report = Report & Val & vbCrLF
    End If
Next

Set ts = oFSO.CreateTextFile (vCleanedLog, ForWriting, True)
ts.Write vReport
MsgBox "Done"
 
Try, just in case.
[tt]
oShell.Run "%comspec% /c ping -a " & vSubnet & x & " >> " & [blue]chr(34) & sTempFile & chr(34)[/blue], 0, True[/tt]

- tsuji
 
Nope that didn't do it either. Wierd thing is when I run it is spawns 2 wscript.exe processes. Its almost like it doesn't wait for the command to run.
 
It must be something else. I don't believe it is the script then. [1] Do a single i to test, [2] Close all files opened in the script at the end.
 
Yeah it is really wierd. Here is what I have just trying to figure out where it is failing. When I run the code below if creates the temp file (runresult.tmp) but with nothing in it and returns false twice even though I am running it once. The server is pingable and when I put the run statement into a command prompt it will create the file and put the resutls into the file showing it is pingable.


Code:
MsgBox IsPingable("server")

Function IsPingable(vServer)
    'On Error Resume Next
    Const OpenAsDefault = -2
    Const FailIfNotExist = 0
    Const ForReading = 1
    
    Set oShell = CreateObject("WScript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
    sTempFile = sTemp & "\runresult.tmp"

    oShell.Run "%comspec% /c ping -n 2 -w 1000 " & vServer & ">" & sTempFile, 0, True

    Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)

    sResults = fFile.ReadAll
    fFile.Close
    'oFSO.DeleteFile (sTempFile)
            
    IsPingable = (InStr(sResults, "TTL=") > 0)
    
    Set oShell = Nothing
    Set oFSO = Nothing

End Function
 
The script itself is not called ping, I hope. Besides, put the 2nd parameter to 1 instead of 0 so you can see what's going on. Also, take out %comspec% /c part of the command line for testing. It is not necessary, but, it does not hurt either.
 
yeah the script was called ping so I renamed it to newping.vbs with this code below. I took out what you said and when I run from the command prompt nothing gets put back to the command prompt but I do get only one false now. But the server is up because I am on the server right now testing this. Still nothing gets wrote to the temp file.

Code:
MsgBox IsPingable("server")

Function IsPingable(vServer)
    On Error Resume Next
    Const OpenAsDefault = -2
    Const FailIfNotExist = 0
    Const ForReading = 1
    
    Set oShell = CreateObject("WScript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
    sTempFile = sTemp & "\runresult.tmp"

    oShell.Run "ping -n 2 -w 1000 " & vServer & ">" & sTempFile, 1, True

    Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)

    sResults = fFile.ReadAll
    fFile.Close
    'oFSO.DeleteFile (sTempFile)
            
    IsPingable = (InStr(sResults, "TTL=") > 0)
    
    Set oShell = Nothing
    Set oFSO = Nothing

End Function
 
No "on error resume next" please. Never.
 
Yeah I took that out but forgot to change it in the code. Just get the input is past end of file error because the file is blank. sorry.
 
It shows the full path to my %temp% variable on the command prompt.
 
This is what I personally use:
Set oShell = WScript.CreateObject ("WScript.shell")
Set oExec = oShell.Exec("ping -n 5 1.0.1.10")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
Do While Not oExec.StdOut.AtEndOfStream
strLine = oExec.StdOut.ReadLine
iPos = InStr(strLine, "%),")
If iPos > 1 Then
a = Split(Left(strLine, iPos - 1), "(")
pLost = a(UBound(a))
Exit Do
End If
Loop
WScript.Echo pLost

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
%temp% = C:\DOCUME~1\WOOLUM~1\LOCALS~1\Temp so looks like there isn't.

PHV yours does the same thing always returns blank even though when going to a command prompt and typing ping server it replies. Plus I don't like how the command box shows unlike what I have that hides it.
 
How about add back some time delay between the .run and .opentextfile. (wscript.sleep 1000, say.)
 
I tried that. I think the Run command is seeing the blank space which is causing it. I change the sTemp to c:\ and that worked. Wierd that windows won't show the space but dos is. Sorry about that. Thanks for the help.
 
You do not need to change the %temp%, as it may affect other applications, or may not. Just use the trick I suggested the early post, I reproduce here:
oShell.Run "%comspec% /c ping -a " & vSubnet & x & " >> " & [blue]chr(34) & sTempFile & chr(34)[/blue], 0, True

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top