Skip to content

Instantly share code, notes, and snippets.

@xr09
Last active November 12, 2018 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xr09/96bc267a0112c435007339dabaf699cc to your computer and use it in GitHub Desktop.
Save xr09/96bc267a0112c435007339dabaf699cc to your computer and use it in GitHub Desktop.
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)
ip name email
192.168.16.1 Jen jen@corp.com
192.168.10.15 Moss moss@corp.com
192.168.16.108 Roy roy@corp.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment