Commit b59261eb authored by chiahetcho's avatar chiahetcho 💬

Initial commit

parents
# -*- coding: utf-8 -*-
from threading import Thread
import queue
import subprocess
import datetime
import time
# defining some global variables
ips = ["138.195.140.143", "192.168.0.1","138.195.155.161","1.1.1.1","158.255.112.228"]
dico_ips = {ips[0]: "firewall-adherents",
ips[1]: "switch-cdr",
ips[2]: "switch-cdr-vu-par-beta-testeurs",
ips[3]: "dns-cloudfare",
ips[4]: "derriere-firewall"}
num_threads = 2
ips_q = queue.Queue()
out_q = queue.Queue()
def thread_pinger(i, q):
# get an IP item from queue
while True:
ip = q.get()
# ping it
ping = subprocess.Popen(
["ping", str(ip)],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
# save the result
out, error = ping.communicate()
with open(dico_ips[ip]+'.txt','a') as file:
ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
file.write(ts + out.decode('cp437'))
if ping.wait() == 0 :
out_q.put(str(ip))
# update queue
q.task_done()
while True:
# start the thread pool
for i in range(num_threads):
worker = Thread(target=thread_pinger, args=(i, ips_q))
worker.setDaemon(True)
worker.start()
# fill queue
for ip in ips:
ips_q.put(ip)
# wait until worker threads are done to exit
ips_q.join()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment