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); }

In reply to Run the same command multiple times in parallel.....Ghetto threading (Windows) by bcarroll

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.