Last active
August 18, 2016 03:42
-
-
Save d4rkcat/69ad1c621d246f8e5b53 to your computer and use it in GitHub Desktop.
multithread linux commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Multi-threading template | |
import argparse, subprocess, signal, Queue | |
from threading import Thread, Lock | |
from sys import stdout | |
from os import getpid, kill | |
class myThread (Thread): | |
def __init__(self, threadID, name, q): | |
Thread.__init__(self) | |
self.threadID = threadID | |
self.name = name | |
self.q = q | |
def run(self): | |
getresponse(self.name, self.q) | |
class Printer(): | |
def __init__(self, data): | |
stdout.write("\r\x1b[K" + data.__str__()) | |
stdout.flush() | |
def getresponse(threadName, q): | |
while not exitFlag: | |
if not workQueue.empty(): | |
queueLock.acquire() | |
work = q.get() | |
queueLock.release() | |
if args.function: | |
print userdefined(work) | |
else: | |
while True: | |
try: | |
command = args.command.replace('$WORK$', work) | |
result = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE).communicate()[0] | |
with open('results', 'a') as outfile: | |
outfile.write(result + '\n') | |
Printer(result + '\n') | |
break | |
except: | |
errorz = ' [X] ' + str(threadName) + ' failed to return! Work - ' + str(work) | |
print errorz + '\n' | |
with open('results', 'a') as outfile: | |
outfile.write(errorz + '\n') | |
else: | |
try: | |
queueLock.release() | |
except: | |
pass | |
return | |
def killpid(signum = 0, frame = 0): | |
print "\r\x1b[K [*] work saved to " + 'results' | |
kill(getpid(), 9) | |
def userdefined(work): | |
return 'I did something with ' + work | |
parser = argparse.ArgumentParser(prog='multit', usage='./multit.py [options]') | |
parser.add_argument('-c', "--command", type=str, help="command to execute eg 'dig +nocmd $WORK$ mx +noall +answer' where $WORK$ is a line from worklist file") | |
parser.add_argument('-f', "--function", action="store_true", help='do the user defined function (userdefined) to the work') | |
parser.add_argument("-w", "--worklist", type=str, help="worklist file") | |
parser.add_argument('-t', "--threads", type=int, help='maximum number of threads') | |
args = parser.parse_args() | |
print ''' _ _ _ _ | |
_ __ ___ _ _| | |_(_) |_ | |
| '_ ` _ \| | | | | __| | __| | |
| | | | | | |_| | | |_| | |_ | |
|_| |_| |_|\__,_|_|\__|_|\__| | |
By d4rkcat | |
''' | |
if not any(vars(args).values()): | |
parser.print_help() | |
exit() | |
worklist = [line.strip() for line in open(args.worklist, 'r')] | |
signal.signal(signal.SIGINT, killpid) | |
queueLock = Lock() | |
workQueue = Queue.Queue(len(worklist)) | |
threads = [] | |
exitFlag = 0 | |
threadID = 1 | |
maxthreads = 40 | |
if args.threads: | |
maxthreads = args.threads | |
if maxthreads > len(worklist): | |
maxthreads = len(worklist) | |
queueLock.acquire() | |
for word in worklist: | |
workQueue.put(word) | |
queueLock.release() | |
while threadID <= maxthreads: | |
tname = str("Thread-") + str(threadID) | |
thread = myThread(threadID, tname, workQueue) | |
thread.start() | |
threads.append(thread) | |
threadID += 1 | |
while not workQueue.empty(): | |
pass | |
exitFlag = 1 | |
for t in threads: | |
t.join() | |
print '\n\n [*] All threads complete, work saved to results' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment