Python Forum
Sent email based on if condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sent email based on if condition
#1
I have script which reads device info from database - IP, LOCATION and STATUS

First it checks if device has status "ACTIVE" if yes than it pings device (incase of any technical problems)

if ping is successful it stores IP of device to list
if ping is unsuccessful than it sends me email

I would like to ask how to send all unresponsive devices in one email? Now i get separate email for each device.

My current code:
# email

def email(ip, location):
    fromaddr = "@gmail.com"
    toaddr = "@gmail.com"
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "Unresponsive device"
    body = """<html><head></head>""" + "IP: " + ip + "<br>" + "Location: " + location+ "</html>"
    msg.attach(MIMEText(body, 'html'))
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "XXXXX")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

# ping

def ping(host):
    result = os.popen(' '.join(("ping", ping.param, host))).read()
    return 'ttl=' in result.lower()

ping.param = "-n 1" if platform.system().lower() == "windows" else "-c 1"

# get devices

mycursor.execute("SELECT ip, location, status FROM devices")
devices = mycursor.fetchall()

ips = []

for device in devices:
    active = device[2]
    if active == "ACTIVE":
        a = ping(device[0])
        if a == True:
            ip = device[0]
            ips.append(ip)
        else:
            email(device[0], device[1])
Reply
#2
You could add it to a list, then send the list after all devices are appended.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  create new column based on condition arvin 12 2,133 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 798 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  How to map two data frames based on multiple condition SriRajesh 0 1,448 Oct-27-2021, 02:43 PM
Last Post: SriRajesh
  Removing some elements from array based on a condition claw91 0 1,483 Oct-27-2020, 03:42 PM
Last Post: claw91
  Pandas, Assign a value in a row, to another column based on a condition klllmmm 6 5,313 Oct-16-2020, 04:43 PM
Last Post: klllmmm
  else condition not called when if condition is false Sandz1286 10 5,739 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,704 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Openpyxl, format color of cells (cols) based on condition. genderbee 0 8,039 Sep-11-2019, 01:05 PM
Last Post: genderbee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020