Created
December 24, 2014 06:48
-
-
Save tdfischer/2bee69fe6ec69538d704 to your computer and use it in GitHub Desktop.
login-notify
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/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