#Ghetto threading - for Windows... #Usage: #ghetto_threads.pl "myCommand.exe /option argument" "path/to/list_of_machines_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(){ 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 thread..\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); }