Last active
May 5, 2020 09:26
-
-
Save heethesh/0412f470bb541661d5d85db5ea5828ec to your computer and use it in GitHub Desktop.
Script to email the system's public IP address
This file contains hidden or 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/python | |
import smtplib | |
from requests import get | |
TO = 'TO_EMAIL_ID' ###### <---- UPDATE | |
FROM = 'FROM_EMAIL_ID' ###### <---- UPDATE | |
# Get public IP | |
ip = get('https://api.ipify.org').text | |
# Setup SMTP server | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.ehlo() | |
server.starttls() | |
server.login(FROM, 'YOUR_FROM_EMAIL_PASSWORD') ###### <---- UPDATE | |
# Send the mail | |
msg = 'Public IP Address: %s' % ip | |
BODY = '\r\n'.join(['To: %s' % TO, | |
'From: %s' % FROM, | |
'Subject: %s' % 'MRSD-PC #X Public IP Address', | |
'', msg]) | |
tries = 1 | |
max_tries = 5 | |
while True: | |
try: | |
server.sendmail(FROM, [TO], BODY) | |
print('Email sent') | |
break | |
except: | |
print('Error sending mail') | |
tries += 1 | |
if tries > max_tries: break | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment