I often need to run a command multiple times (hundreds/thousands), and instead of using a really long batchfile and waiting for each command to run sequentially, I came up with this script (ghetto_threads.pl).

The script takes two arguments from the command-line.
1 - The command to execute(use quotes if the command contains spaces)
2 - a text file containing additional arguments (typically a list of hostnames) to be passed to the command specified.(use quotes if the path/filename contains spaces)

I mainly use this for things like ping, nslookup,WMI queries, etc..., but I'm sure someone will have another use I haven't thought of.

I know it is not pretty.... but it works for me...

#Ghetto threading - for Windows... #Usage: #ghetto_threads.pl "myCommand.exe /option argument" "path/to/list_of_m +achines_to_execute_against.txt" use warnings; use strict; my $maxThreads = 50; #maximum number of commands running at one time my $cmd = $ARGV[0]; my $list = $ARGV[1]; print "Command: $cmd\n"; open(F, $list) or die "$!\n"; while(<F>){ chomp $_; #remove the newline character spawn($cmd,$_); #create a new thread } close F; sub spawn{ #Create a new instance of $cmd my $cmd = shift; my $arg = shift; return unless $arg; #if the current number of running threads # is less than the maximum allowed, run the command. if ( processAccounting($cmd) < $maxThreads ){ if ($cmd =~ /\.vbs$/){ # if the command specified is a .vbs file, suppress the VBScript + logo text print "$cmd($arg)->" . system("start /B $cmd //NoLogo $arg") . " +\n"; } else { print "$cmd($arg)->" . system("start /B $cmd $arg") . "\n"; } } else { print "$cmd($arg)->No threads available.\nWaiting for available th +read..\n"; sleep(1); spawn($cmd,$arg); } } sub processAccounting{ #List the number of $cmd instances my $procCount = 0; my $cmd = shift; use Win32::Process::List; my $P = Win32::Process::List->new(); my %list = $P->GetProcesses(); foreach my $key ( keys %list ) { # $list{$key} is now the process name and $key is the PID if ( ($cmd =~ /vbs$/) && ($list{$key} =~ /wscript\.exe/ || $list{$ +key} =~ /cscript\.exe/) ){ print "$list{$key} has PID $key\n"; $procCount++; } elsif ($list{$key} =~ /$cmd/){ print "$list{$key} has PID $key\n"; $procCount++; } } print "Threads: $procCount\n"; return($procCount); }
  • Comment on Run the same command multiple times in parallel.....Ghetto threading (Windows)
  • Download Code