Solved

Why wont each address be echoed to the file ?

Posted on 2004-09-13
6
168 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:123dec45
  • 3
  • 3
6 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 12045284
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
0
 
LVL 1

Author Comment

by:123dec45
ID: 12045440
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 ?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 12045547
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")
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:123dec45
ID: 12052189
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
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 100 total points
ID: 12052592
The shellwait should do it for you then.  It will run the the command and wait for it to complete, regardless of how short or long it takes...
0
 
LVL 1

Author Comment

by:123dec45
ID: 12076955
that worked fine,
thanks for your help :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now