Skip to content

Instantly share code, notes, and snippets.

@bohops
Created September 4, 2021 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohops/c45faef197e4e1c86a9d7c08c9c1bb9e to your computer and use it in GitHub Desktop.
Save bohops/c45faef197e4e1c86a9d7c08c9c1bb9e to your computer and use it in GitHub Desktop.
Quick & Dirty Ping Monitor + Email Report
# Quick & Dirty Ping Monitor + Email Report
# -----------------------------------------
# Code Credits:
# - Ping Server In Python: https://stackoverflow.com/questions/2953462/pinging-servers-in-python
# - Simple Python Server Monitor: https://github.com/brendancarlson/Simple-Python-Server-Monitor/blob/master/monitor.py
# -----------------------------------------
# Basic Usage: python3 ping_monitor.py
# Cron Usage:
# - Set for every hour to avoid overwhelming SMTP server thresholds
# - crontab -i
# - Command: i
# - Insert text: 0 * * * * /path/to/python3 /path/to/ping_monitor.py
# - Command: Shift-ZZ
# - contrab -l
import platform
import subprocess
import smtplib
# Main settings
MONITORED_HOSTS = ['10.10.10.2' , '10.10.10.3'] #Add hosts to monitor in this list
SENDER = 'sender@email.something'
RECIPIENT = 'recipient@email.something'
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
SUBJECT = 'Host(s) Offline Report'
SMTP_USER = 'sender@gmail.com'
SMTP_PASS = 'P@ssw0rd'
# Source: https://stackoverflow.com/questions/2953462/pinging-servers-in-python
def DoPing(host):
param = '-n' if platform.system().lower()=='windows' else '-c'
command = ['ping', param, '1', host]
return subprocess.call(command) == 0
# Source: https://github.com/brendancarlson/Simple-Python-Server-Monitor/blob/master/monitor.py
# License:
#The MIT License (MIT)
#
#Copyright (c) 2013 Brendan Carlson (https://github.com/brendancarlson) & Darayus (https://github.com/Darayus)
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
def SendEmail(body):
sender = SENDER
recipient = RECIPIENT
subject = SUBJECT
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
session.login(SMTP_USER, SMTP_PASS)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
# Main
def main():
send_flag = False
results = ""
for host in MONITORED_HOSTS:
status = DoPing(host)
if (status is False):
send_flag = True
results += "<br>[-] Host Down: " + host + "</br>"
else:
results += "<br>[+] Host Up: " + host + "</br>"
if (send_flag):
SendEmail(results)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment