Last active
November 12, 2018 15:41
-
-
Save xr09/96bc267a0112c435007339dabaf699cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import csv | |
import datetime | |
import subprocess | |
import logging | |
from concurrent.futures import ThreadPoolExecutor | |
WORKERS_FILE = 'workers.csv' | |
logging.basicConfig( | |
level=logging.INFO, | |
format="%(asctime)s [%(threadName)s] [%(levelname)s] %(message)s", | |
handlers=[ | |
logging.FileHandler("knocker.log", mode='a', encoding=None, delay=False), | |
logging.StreamHandler(sys.stdout) | |
]) | |
def get_workers(wfile): | |
workers = [] | |
with open(wfile) as f: | |
reader = csv.DictReader(f) | |
for line in reader: | |
workers.append(line) | |
return workers | |
def check_host(worker): | |
status = subprocess.call('ping -w1 -c1 {host}'.format(host=worker['ip']), shell=True) | |
if status == 0: | |
# print("{ip} is {status}".format(ip=worker['ip'], status=status)) | |
"the poor bastard is online!!" | |
dateo = datetime.datetime.now().ctime() | |
logging.info("{w[name]} was online at {date}".format(w=worker, date=dateo)) | |
mail_warning(worker, dateo) | |
def mail_warning(worker, dateo): | |
mail_body = """Hello {w[name]} | |
You've been caught working after hours. | |
Your IP address ({w[ip]}) was found online at "{date}" | |
If you could get a life that'd be great! | |
Yours truly, u/Mahesh1910 :)""".format(w=worker, date=dateo) | |
#print(mail_body) | |
if __name__ == '__main__': | |
workers = get_workers(WORKERS_FILE) | |
with ThreadPoolExecutor(max_workers=None) as executor: | |
executor.map(check_host, workers) |
This file contains hidden or 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
ip | name | ||
---|---|---|---|
192.168.16.1 | Jen | [email protected] | |
192.168.10.15 | Moss | [email protected] | |
192.168.16.108 | Roy | [email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment