Skip to content

Instantly share code, notes, and snippets.

@abyesilyurt
Last active April 28, 2018 07:50
Show Gist options
  • Save abyesilyurt/06ae05c9f66ee1c7312fe23536a97db0 to your computer and use it in GitHub Desktop.
Save abyesilyurt/06ae05c9f66ee1c7312fe23536a97db0 to your computer and use it in GitHub Desktop.
Script to automatically email IP and the hostname of the computer
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 28 09:23:30 2018
@author: abyesilyurt
"""
#!/usr/bin/env python
# modified from http://elinux.org/RPi_Email_IP_On_Boot_Debian
import subprocess
import smtplib
import socket
from email.message import EmailMessage
# Change to your own account information
to = 'XXX@gmail.com'
gmail_user = 'XXX@gmail.com'
gmail_password = 'password'
smtpserver = smtplib.SMTP('smtp.gmail.com:587')
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_password)
#%%
import subprocess
result_hostname = str( subprocess.run(['hostname'], stdout=subprocess.PIPE).stdout[0:-1].decode("utf-8") )
result_ip =str( subprocess.run(['hostname', '-I'], stdout=subprocess.PIPE).stdout[0:-1].decode("utf-8") )
msg = MIMEText(result_ip)
msg['Subject'] = 'IP address of ' + result_hostname
msg['From'] = gmail_user
msg['To'] = gmail_user
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