Created
March 7, 2017 18:00
-
-
Save wtrssn/51815b925d09528c5133162e10cae131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import socket | |
import os | |
import subprocess | |
import smtplib | |
from email.mime.text import MIMEText | |
import datetime | |
gw = os.popen("ip -4 route show default").read().split() | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.connect((gw[2], 0)) | |
ipaddr = s.getsockname()[0] | |
gateway = gw[2] | |
host = socket.gethostname() | |
print ("IP:", ipaddr, " GW:", gateway, " Host:", host) | |
# Change to your own account information | |
to = 'me@example.com' | |
gmail_user = 'test@gmail.com' | |
gmail_password = 'yourpassword' | |
smtpserver = smtplib.SMTP('smtp.gmail.com', 587) | |
smtpserver.ehlo() | |
smtpserver.starttls() | |
smtpserver.ehlo | |
smtpserver.login(gmail_user, gmail_password) | |
today = datetime.date.today() | |
# Very Linux Specific | |
arg='ip route list' | |
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) | |
data = p.communicate() | |
split_data = data[0].split() | |
ipaddr = split_data[split_data.index('src')+1] | |
my_ip = 'Your ip is %s' % ipaddr | |
msg = MIMEText(my_ip) | |
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y') | |
msg['From'] = gmail_user | |
msg['To'] = to | |
smtpserver.sendmail(gmail_user, [to], msg.as_string()) | |
smtpserver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment