Link to home
Start Free TrialLog in
Avatar of 123dec45
123dec45

asked on

Why wont each address be echoed to the file ?

Scenario
1 flat input file contains ip addresses :
190.1.1.1.
172.24.212.1
190.1.1.2

2 audit files

todo: read each ip address in and attempt ping. Log result into a log file containing current address then write contents out to a log file containing all entries .

What i get is all addresses wrtitten out to control file out.txt but only the result of the last ping to the control file pinglog.txt which would assume, the shell command is only being passed once.

Private Sub Command1_Click()

Dim objname As String

Open "C:\wip\adlist.txt" For Input As #1
Open "C:\wip\out.txt" For Output As #2
Open "C:\wip\pinglog.txt" For Output As #3


Do While Not EOF(1)
     
Input #1, objname

Print #2, objname

retval = Shell("cmd /c c:\windows\system32\ping.exe " & objname & " -n 10  >> c:\wip\pinglog.txt", 0)

Loop

Close #1
Close #2
Close #3

End Sub
Avatar of sirbounty
sirbounty
Flag of United States of America image

Hi 123dec45,
First of all, this line
Open "C:\wip\pinglog.txt" For Output As #3
isn't even needed in your code.

You also have a trailing period after your first ip address (maybe a type-o)

Try

Dim objname As String
Open "C:\wip\adlist.txt" For Input As #1
Open "C:\wip\out.txt" For APPEND As #2

Do While Not EOF(1)
 Input #1, objname
 Print #2, objname
 Shell ("cmd /c ping.exe " & objname & " -n 10  >> c:\wip\pinglog.txt")
Loop
Avatar of 123dec45
123dec45

ASKER

hmm nearly;-
yep it was a type-o !
i have 3 enteries in my adlist file
i get 3 records in my out.txt :-)
i get only 1 entry in my pinglog :-(
I have manualy tested all 3 entries nad they reply fine, I have also reduced the -n to 1 to get a quicker response. Interestingly, to see the data written to out.txt you have to halt the code. I wonder if there is something stuck that is not being flushed between each Shell command ?
It's probably running over itself - try using shellwait instead:

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
    Do
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function

Shellwait ("cmd /c ping.exe" & objname & " -n 1 >> c:\wip\pinglog.txt")
yep i think your right, i also came accross this whilst on the train last night trying some DIR's. When I put a sleep for 5 seconds between each loop, the problem went away. That said I am not certain a pause for x seconds is the best way. The example I give I am doing a fairly simple pings, in real life it will run some fairly long sub routines depending on the result of the ping, with complex routing it could take some time to learn a new route over several hops. Maybe a better way could be a trigger, either writing to the registry or a file to show the loop starting and stopping ? I have 3500 machines to diagnose so any seconds I can save will make a real difference. Thanks for any other suggestions you have. Regards
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that worked fine,
thanks for your help :-)