Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
login-notify
#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
import os
import sys
from twilio.rest import TwilioRestClient
import imp
config = imp.load_source('login_notify.config', '/etc/sysconfig/login-notify')
if 'PAM_RHOST' in os.environ:
host = os.environ['PAM_RHOST']
else:
host = os.environ['PAM_TTY']
user = os.environ['PAM_USER']
msgText = """
A new login has occured for the user '%s', originating from '%s'.
""" % (user, host)
msg = MIMEText(msgText)
msg['Subject'] = "New login for %s from %s" % (user, host)
msg['To'] = config.EMAIL_FROM
msg['From'] = "login-notify"
s = smtplib.SMTP('localhost')
s.sendmail('login-notify', config.EMAIL_TO, msg.as_string())
s.quit()
sms = TwilioRestClient(config.TWILIO_ACCOUNT, config.TWILIO_TOKEN)
sms.messages.create(to_=config.TWILIO_SMS_TO, from_=config.TWILIO_SMS_FROM, body=msgText)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment