Multithread Remote SSH with Python: mudanças entre as edições
Ir para navegação
Ir para pesquisar
(→run.py) |
|||
Linha 38: | Linha 38: | ||
=runp.py= | =runp.py= | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
#!/usr/bin/env python | |||
from src.activeInventory import ActiveInventory | |||
from src.configs import Config | |||
from src.argmenu import ArgMenu | |||
import queue | |||
import threading | |||
import socket | |||
exitFlag = 0 | |||
class MyThread(threading.Thread): | |||
def __init__(self, thread_id, name, q): | |||
threading.Thread.__init__(self) | |||
self.threadID = thread_id | |||
self.name = name | |||
self.q = q | |||
def run(self): | |||
print("Starting " + self.name) | |||
process_data(self.name, self.q) | |||
print("Exiting " + self.name) | |||
def is_open(ip, port): | |||
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
location = (ip, int(port)) | |||
result_of_check = a_socket.connect_ex(location) | |||
if result_of_check == 0: | |||
return True | |||
else: | |||
return False | |||
def process_data(thread_name, q): | |||
print(thread_name) | |||
while not exitFlag: | |||
queueLock.acquire() | |||
if not workQueue.empty(): | |||
server = q.get() | |||
queueLock.release() | |||
argsmenu = ArgMenu() | |||
args = argsmenu.get_args() | |||
active_inventory = ActiveInventory(args.outdir) | |||
credfile = Config(args.cred) | |||
credentials = credfile.get_data("credentials") | |||
control = 1 | |||
if args.timeout is not None: | |||
timeout = args.timeout | |||
else: | |||
timeout = 10 | |||
if is_open(server.replace("\n", ""), 22) == True: | |||
for credential in credentials: | |||
# time.sleep(1) | |||
if control == 0: | |||
break | |||
if active_inventory.run_remote_command(command=args.cmd, | |||
username=credential["username"], | |||
password=credential["password"], | |||
prefix=args.prefix, | |||
server=server.replace("\n", ""), | |||
timeout=timeout) == 0: | |||
control = 0 | |||
else: | |||
queueLock.release() | |||
if __name__ == "__main__": | |||
argsmenu = ArgMenu() | |||
args = argsmenu.get_args() | |||
queueSize = 6000 | |||
# queueSize = sum(1 for line in open(str(args.servers).replace("\n", ""))) | |||
if args.threads is not None: | |||
threadCount = int(args.threads) | |||
else: | |||
threadCount = 10 | |||
threadList = range(1, threadCount) | |||
queueLock = threading.Lock() | |||
workQueue = queue.Queue(queueSize) | |||
# workQueue = queue.Queue(int(queueSize) + 1) | |||
threads = [] | |||
threadID = 1 | |||
# Fill the queue | |||
queueLock.acquire() | |||
with open(args.servers, "r") as servers: | |||
for server in servers: | |||
workQueue.put(str(server).replace("\n", "")) | |||
queueLock.release() | |||
# Create new threads | |||
for tName in threadList: | |||
thread = MyThread(threadID, tName, workQueue) | |||
thread.start() | |||
threads.append(thread) | |||
threadID += 1 | |||
# Wait for queue to empty | |||
while not workQueue.empty(): | |||
pass | |||
# Notify threads it's time to exit | |||
exitFlag = 1 | |||
# Wait for all threads to complete | |||
for t in threads: | |||
t.join() | |||
print("Exiting Main Thread") | |||
# main.remoteExecution() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Edição das 18h08min de 25 de janeiro de 2022
run.py
#!/usr/bin/env python
from src.activeInventory import ActiveInventory
from src.configs import Config
from src.argmenu import ArgMenu
import time
class main:
def remoteExecution():
argsmenu = ArgMenu()
args = argsmenu.get_args()
activeInventory = ActiveInventory(args.outdir)
credfile = Config(args.cred)
credentials = credfile.get_data("credentials")
with open(args.servers, "r") as servers:
control = 1
count = 0
for server in servers:
count = count + 1
if control == 0:
control = 1
continue
for credential in credentials:
# time.sleep(1)
if control == 0:
break
if activeInventory.run_remote_command(command=args.cmd,
username=credential["username"],
password=credential["password"],
prefix=args.prefix,
server=server.replace("\n", "")) == 0:
control = 0
if __name__ == "__main__":
main.remoteExecution()
runp.py
#!/usr/bin/env python
from src.activeInventory import ActiveInventory
from src.configs import Config
from src.argmenu import ArgMenu
import queue
import threading
import socket
exitFlag = 0
class MyThread(threading.Thread):
def __init__(self, thread_id, name, q):
threading.Thread.__init__(self)
self.threadID = thread_id
self.name = name
self.q = q
def run(self):
print("Starting " + self.name)
process_data(self.name, self.q)
print("Exiting " + self.name)
def is_open(ip, port):
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
location = (ip, int(port))
result_of_check = a_socket.connect_ex(location)
if result_of_check == 0:
return True
else:
return False
def process_data(thread_name, q):
print(thread_name)
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
server = q.get()
queueLock.release()
argsmenu = ArgMenu()
args = argsmenu.get_args()
active_inventory = ActiveInventory(args.outdir)
credfile = Config(args.cred)
credentials = credfile.get_data("credentials")
control = 1
if args.timeout is not None:
timeout = args.timeout
else:
timeout = 10
if is_open(server.replace("\n", ""), 22) == True:
for credential in credentials:
# time.sleep(1)
if control == 0:
break
if active_inventory.run_remote_command(command=args.cmd,
username=credential["username"],
password=credential["password"],
prefix=args.prefix,
server=server.replace("\n", ""),
timeout=timeout) == 0:
control = 0
else:
queueLock.release()
if __name__ == "__main__":
argsmenu = ArgMenu()
args = argsmenu.get_args()
queueSize = 6000
# queueSize = sum(1 for line in open(str(args.servers).replace("\n", "")))
if args.threads is not None:
threadCount = int(args.threads)
else:
threadCount = 10
threadList = range(1, threadCount)
queueLock = threading.Lock()
workQueue = queue.Queue(queueSize)
# workQueue = queue.Queue(int(queueSize) + 1)
threads = []
threadID = 1
# Fill the queue
queueLock.acquire()
with open(args.servers, "r") as servers:
for server in servers:
workQueue.put(str(server).replace("\n", ""))
queueLock.release()
# Create new threads
for tName in threadList:
thread = MyThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print("Exiting Main Thread")
# main.remoteExecution()