0

I found these codes from various websites: ping.py and conf.py. It's working fine. I need to combine these files into a single file.

ping.py:

#!/usr/bin/env python

import smtplib
import pyping
from conf import settings, sites
import time
import datetime

"""Sends an e-mail to the specified recipient."""
sender = settings["monitor_email"]
recipient = settings["recipient_email"]
subject = settings["email_subject"]
headers = ["From: " + sender,
    "Subject: " + subject,
    "To: " + recipient,
    "MIME-Version: 1.0",
    "Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(settings["monitor_server"],
settings["monitor_server_port"])
session.ehlo()
session.login(settings["monitor_email"], settings["monitor_password"])
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')

   for site in sites:
   checker = pyping.ping(site)
   # The site status changed from it's last value, so send an email

   if checker.ret_code == 0:

    # The site is UP
    body = "%s This Server is up %s" % (site, st)
    session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
else:
    # The site is Down
    body = "%s This Server is down %s" % (site, st)
    session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)

  session.quit()

conf.py:

sites = (
"192.168.1.1",
"192.168.2.1",
"192.168.3.1",
)

settings = {
"recipient_email": 'tomail@domain.com',
"monitor_email": 'frommail@domain.com',
"monitor_password": 'password',

# Leave as it is to use gmail as the server
"monitor_server": 'frommail@domain.com',
"monitor_server_port": 587,

# Optional Settings
"email_subject": 'Server Monitor Alert'
}

How to integrate conf.py into ping.py so I can get output from running a single file?

anothernode
  • 5,100
  • 13
  • 43
  • 62
Maximus
  • 29
  • 2
  • 9

2 Answers2

2

You need to copy the entire file since "recipient_email": 'tomail@domain.com' is not valid variable definition.

Replace from conf import settings, sites with the file contents, or better, define the variables as you want them.

For example, rather than

sender = settings["monitor_email"]
recipient = settings["recipient_email"]
subject = settings["email_subject"]

Do

sender = "frommail@domain.com"
recipient = "tomail@domain.com"
subject = 'Server Monitor Alert'
# TODO: Define other values

when i put recipient = tomail@gmail.com its not working

You need quotes around string variables...

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • These lines produce errors session = smtplib.SMTP(settings["monitor_server"], session.login(settings["monitor_email"], settings["monitor_password"]) – Maximus Apr 22 '18 at 08:17
  • What about it? Again, use the other variable names, not those from `settings`. In fact, delete `settings` variable entirely. – OneCricketeer Apr 22 '18 at 08:18
  • As written, `TODO`... You must define the other values, or in-line them. `smtplib.SMTP('frommail@domain.com', ...` – OneCricketeer Apr 22 '18 at 08:20
  • And buddy this works fine in windows but when i run it on linux bash it gives me error ./ping.py: line 1: $'\r': command not found from: too many arguments ./ping.py: line 7: $'\r': command not found ./ping.py: line 8: settings: command not found ./ping.py: line 12: $'\r': command not found ./ping.py: line 14: monitor_server:: command not found ./ping.py: line 16: $'\r': command not found ./ping.py: line 21: syntax error near unexpected token `(' – Maximus Apr 22 '18 at 08:26
  • I don't know why bash is interpreting your file directly. Do `python ping.py` – OneCricketeer Apr 22 '18 at 08:30
1

Copy and paste the complete content of conf.py into ping.py just after the

import datetime

line. Then remove the line

from conf import settings, sites

from ping.py.

Note that doing so is usually quite the opposite of what you should be doing in terms of good coding style. Generally, you want to modularize your code, while this means taking (more or less) modular code and turning it into a big and unwieldy clump.

anothernode
  • 5,100
  • 13
  • 43
  • 62