-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniqueWifi.py
45 lines (37 loc) · 1.89 KB
/
uniqueWifi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
def main():
import datetime
import smtplib
from subprocess import check_output
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define the email addresses to be used
fromEmailAddress = ''
toEmailAddress = '' #multiple email addresses comma separated
# find the year and month of last month and place it into formattedLastMonth variable
today = datetime.date.today()
firstDay = today.replace(day=1)
lastMonth = firstDay - datetime.timedelta(days=1)
formattedLastMonth = lastMonth.strftime("%Y%m")
print(formattedLastMonth)
# using the data from last month, find the number of wireless devices and set the value of output to that value and convert it to a string
# format below
# MMM DD hh:mm:ss serverName dhcpd[#####]: IP: ipAddr %%% HW: macAddressColonDelim %%% RELAY: routerIP %%% INTERFACE: interfaceID %%% HOSTNAME: hostname %%% VENDOR: operatingSystemID
output = check_output("/bin/grep \"IP: 10\.6\.\" /home/rsyslog/dhcp*-" + str(formattedLastMonth) + "*.log | /usr/bin/awk \'{print $10}\' | /usr/bin/sort -u | /usr/bin/wc -l", shell=True)
output = str(output,'utf-8')
print(output)
# create the string to be sent in email
uniqueWifiDevicesString = "The number of unique WIFI devices for " + str(formattedLastMonth) + " is: " + output
# set up email MIME message and attach the text version of the string
msg = MIMEMultipart()
msg['From'] = fromEmailAddress
msg['To'] = toEmailAddress
msg['Subject'] = 'Unique WiFi Devices for ' + str(formattedLastMonth)
part1 = MIMEText(uniqueWifiDevicesString,'plain')
msg.attach(part1)
# send the email out to recipients
s=smtplib.SMTP('localhost')
s.sendmail(msg["From"],msg["To"].split(","),msg.as_string())
if __name__ == "__main__":
main()