Page 1 of 1 1
Topic Options
#45256 - 2003-09-16 06:21 PM Speeding up a script
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
I use the following code at times to send a message to PC's on the network.


Break ON

If RedirectOutput("c:\mess_results.TXT",0) = 0
  If Open (1,"CorpPCnames.TXT",0) = 0
    ?"Began sending messages at "@TIME
    $dest = ReadLine(1)
    While @ERROR = 0 
      $msg='Message goes here'
      SendMessage($dest,$msg)
      ? 'Sending message to $dest... = '+@ERROR+' - '+@SERROR 
      $dest = ReadLine(1)
    Loop
    ?"Completed sending messages at "@TIME
    $RC = Close (1)
  EndIf
EndIf
If
  RedirectOutput("")=0 
EndIf
Exit 0



The problem is that it is fairly slow. If a PC is not on the network at the time the message is sent, it slows down considerably. Issuing a ping before sending and then checking for a response takes longer than waiting for the SENDMESSAGE to time out.

Any suggestions?

Top
#45257 - 2003-09-16 06:29 PM Re: Speeding up a script
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
How about...

ping -n 1 $sTarget

Top
#45258 - 2003-09-16 06:32 PM Re: Speeding up a script
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
if the computers your are pinging are in a local fast network. use the ping -w ## to adjust the wait time for time outs.
Top
#45259 - 2003-09-16 07:03 PM Re: Speeding up a script
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
hah.. how about a parallel sendmessage?
code:
	$msg='whatever to send'
$file=readfile('CorpPCnames.TXT')
for each $dest in $file
run 'cmd /c net send '+trim($dest) +' '+ $msg
next

BTW, readfile() is a UDF... and I prefer my version over Lonk's

[ 16. September 2003, 19:05: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#45260 - 2003-09-16 09:16 PM Re: Speeding up a script
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Radimus,

I assume by reading the entire file and using "RUN", this will open a new instance of the command intrepter for each message. Since RUN doesn't wait for anything before continuing, would you be able to capture any type of feedback as to whether or not the send was successful?

Also, what happens when you send this to 1000+ machines? Are you going to bring your Admin workstation to it's knees?

Top
#45261 - 2003-09-16 10:06 PM Re: Speeding up a script
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
it actually uses the same cmd window, if you leave it open you will see the "message sent" and errors show up there

if you want to a counter could be inserted in the for each that sleeps for 10 or 15 seconds for every 20 or 30 or whatever number of messages

something like:
code:
 	$counter=0
$msg='whatever to send'
$file=readfile('CorpPCnames.TXT')
for each $dest in $file
run 'cmd /c net send '+trim($dest) +' '+ $msg
$counter=$counter+1
if $counter=25
sleep 10
$counter=0
endif
next

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#45262 - 2003-09-18 12:17 AM Re: Speeding up a script
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Radimus,

Your example worked pretty well. It was definately faster. It would be nice if you could actually record and or time the result messages but I'm not sure how since the results are displayed in the command window after the script has actually finished.

Also, it never returns to a "c" prompt, so if you're sending to a large number of PC's, it's sometimes difficult to tell if it has completed or if it is hung.

Top
#45263 - 2003-09-18 12:20 AM Re: Speeding up a script
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
heh.
skip over to wkix32 believers!
instead of trying to get hang of kix's console use simple:
$rc=redirectoutput("%temp%\@date@time")
_________________________
!

download KiXnet

Top
#45264 - 2003-09-17 02:01 PM Re: Speeding up a script
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=2;t=004563#000000

http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=2;t=004563#000000

but it doesn't do parallel

[ 17. September 2003, 14:07: Message edited by: Radimus ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#45265 - 2003-09-17 02:40 PM Re: Speeding up a script
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
posted paraller sender but remember that I just wrapped it up in 10mins in postprep edit window.
so, it most likely does not work perfect.
_________________________
!

download KiXnet

Top
#45266 - 2003-09-17 02:59 PM Re: Speeding up a script
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ja basta, weren't you the one that wrote the first-ever, multi-threaded, massively parallel kixtart script ? If memory serves, something to do with registry updates ... man, you guys really push kixtart to the bleeding edge [Wink]
Top
#45267 - 2003-09-17 03:04 PM Re: Speeding up a script
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Funny you should say that...

Here is a generic parallel multi tasking script, which can easily be changed for most (any?)application:


Break ON
; vim>600:filetype=kix sw=3 ts=3
; vim>600:fdm=marker fdc=4

; Generic multi task launcher
$gNull=SetOption("Explicit","ON")
$gNull=SetOption("ASCII","ON")
$gNull=SetOption("WrapAtEol","ON")

Global $gControlFile
Dim $iMaxThreads,$sKixCommand

;********************************************************************************************
;************************BEGIN CUSTOMISED VARIABLE DEFINITION********************************
; Change the next three variable definitions to fit local requirements *
$gControlFile=@SCRIPTDIR+"\MultiTask.ini" ; Main control file. *
$iMaxThreads=10 ; Maximum child processes to start *
$sKixCommand="kix32.exe" ; KiXtart command used to start child processes *
;*************************END CUSTOMISED VARIABLE DEFINITION*********************************
;********************************************************************************************

; Decide if this is a child or parent task
If IsDeclared($gChildName) ; Child task.
$gNull=WriteProfileString($gControlFile,"STATUS",$gChildName,udfChildTask($gChildName,$gChildParameter))
Else ; Parent task
Dim $sChildParameter ; New thread parameter
Dim $sChildName ; Child name
Dim $iChildCount ; How many child processes are active

"Parent starting, maximum threads="+$iMaxThreads+@CRLF

; Set up control file
If RedirectOutput($gControlFile,1)
"FATAL: Cannot open control file for writing!"+@CRLF
" Error: "+@ERROR+", "+@SERROR+@CRLF
Exit 1
EndIf
"[STATUS]"+@CRLF
"[ACKNOWLEDGED]"+@CRLF
$gNull=RedirectOutput("")

If udfInitialiseParent()
"ERROR: Cannot initialise parent process."+@CRLF
Exit 1
Else
"Parent initialisation complete. Let's go ta woik..."+@CRLF

; Get each child process
$sChildParameter=udfEnumChildParameter()
While $sChildParameter
$sChildName="Child_"+Right("000000"+(Val(SubStr($sChildName,7))+1),6)
; Wait until a slot is available
While $iChildCount=$iMaxThreads $iChildCount=$iChildCount-udfCheckStatus() Loop
; Launch child
$iChildCount=$iChildCount+1
Run $sKixCommand+' '+@SCRIPTDIR+'\'+@SCRIPTNAME+' $$gChildName='+$sChildName+' $$gChildParameter="'+$sChildParameter+'"'
$sChildParameter=udfEnumChildParameter()
Loop

; Wait for all tasks to complete
"All child tasks started - waiting for "+$iChildCount+" to complete"+@CRLF
While $iChildCount $iChildCount=$iChildCount-udfCheckStatus() Loop
"Run completed."+@CRLF
EndIf

Exit 0

; This routine will check the status of running jobs and return the number that have completed.
Function udfCheckStatus() ; {{{
Dim $sChildName,$sStatus
$udfCheckStatus=0
For Each $sChildName In Split(ReadProfileString($gControlFile,"STATUS",""),Chr(10))
If $sChildName
$udfCheckStatus=$udfCheckStatus+1
$sStatus=ReadProfileString($gControlFile,"STATUS",$sChildName)
$sChildName+" reports completion status: "+$sStatus+@CRLF
$gNull=WriteProfileString($gControlFile,"STATUS",$sChildName,"")
$gNull=WriteProfileString($gControlFile,"ACKNOWLEDGED",$sChildName,$sStatus)
EndIf
Next
; Sleep a little to give up CPU between checks *IF* no jobs have completed.
If Not $udfCheckStatus Sleep 0.2 EndIf
EndFunction ; }}}

;----------------------------------------------------------------------------
;---------------------BEGIN CUSTOMISATION SECTION----------------------------
; Change the UDFS below this point to customise the multithreaded process.
; The demo code lines are all labelled "*DEMO*", change or remove as needed.

; Parent initialisation.
; Use this to set up structures and variables for the udfEnumChildParameter()
; Don't forget to make variables global if they are going to be used in the
; subsequent functions.
;
; The *DEMO* initialisation code will open a text input file.
;
Function udfInitialiseParent() ; {{{
$udfInitialiseParent=0 ; Default to "OK" state
Global $gfhInput ; *DEMO*
$gfhInput=FreeFileHandle() ; *DEMO*
If Not $gfhInput ; *DEMO*
"Fatal: Cannot allocate file handle"+@CRLF ; *DEMO*
$udfInitialiseParent=1 ; *DEMO*
Exit 1 ; *DEMO*
EndIf ; *DEMO*
If Open($gfhInput,"input.txt") ; *DEMO*
"Fatal: Cannot open input file"+@CRLF ; *DEMO*
$udfInitialiseParent=1 ; *DEMO*
Exit 1 ; *DEMO*
EndIf ; *DEMO*
Exit 0
EndFunction ; }}}

; Enumerate child parameter.
; Each time this function is called it should return a value which will be
; passed to the new thread as a parameter. It should return a null value when
; all thread parameters have been exhausted.
;
; The *DEMO* code will return a line from the input file opened in the parent
; initialisation each time it is called
;
Function udfEnumChildParameter() ; {{{
$udfEnumChildParameter="" ; Default to "List exhausted" state

$udfEnumChildParameter=ReadLine($gfhInput) ; *DEMO*
If @ERROR $udfEnumChildParameter="" EndIf ; *DEMO*
Exit 0
EndFunction ; }}}

; Child task.
; When a new thread is launched this function is called with the child parameter
; The function should return a string representing the status to be returned to
; the parent.
;
; The *DEMO* code just sleeps a random number of seconds, then returns the
; paramater in the status
Function udfChildTask($sName,$sParameter) ; {{{
$udfChildTask="0 OK" ; Default success message

Srnd(@MSECS*1000+Val(Right($sName,6))) ; *DEMO*
Sleep 3+Rnd(15) ; *DEMO*
$udfChildTask="0 '"+$sParameter+"'" ; *DEMO*
Exit 0
EndFunction ; }}}


Top
#45268 - 2003-09-17 03:08 PM Re: Speeding up a script
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Oops!

Forgot to mention that it will display the status of each completed job on the screen, and also leave the status in the "ini" file once it has completed.

Top
#45269 - 2003-09-17 05:58 PM Re: Speeding up a script
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, forgot too [Big Grin]
this one also leaves it there [Wink]

but, the difference with the one shawn remembers is that it used registry [Razz]
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 874 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.142 seconds in which 0.102 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org