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": '[email protected]',
"monitor_email": '[email protected]',
"monitor_password": 'password',

# Leave as it is to use gmail as the server
"monitor_server": '[email protected]',
"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?

3
  • 1
    What do you mean with combin ? Cant you gust copy and paste the code? Commented Apr 22, 2018 at 7:38
  • This question already has an answer at stackoverflow.com/a/16604453 Commented Apr 22, 2018 at 7:39
  • when i put recipient = [email protected] its not working , I want to pass values directly. Commented Apr 22, 2018 at 7:44

2 Answers 2

2

You need to copy the entire file since "recipient_email": '[email protected]' 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 = "[email protected]"
recipient = "[email protected]"
subject = 'Server Monitor Alert'
# TODO: Define other values

when i put recipient = [email protected] its not working

You need quotes around string variables...

Sign up to request clarification or add additional context in comments.

5 Comments

These lines produce errors session = smtplib.SMTP(settings["monitor_server"], session.login(settings["monitor_email"], settings["monitor_password"])
What about it? Again, use the other variable names, not those from settings. In fact, delete settings variable entirely.
As written, TODO... You must define the other values, or in-line them. smtplib.SMTP('[email protected]', ...
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 `('
I don't know why bash is interpreting your file directly. Do python ping.py
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.