Skip to content

Instantly share code, notes, and snippets.

@weber
Last active December 28, 2015 09:39
Show Gist options
  • Select an option

  • Save weber/7480081 to your computer and use it in GitHub Desktop.

Select an option

Save weber/7480081 to your computer and use it in GitHub Desktop.
SSH уведомление об авторизации
#!/usr/bin/env python
import smtplib, os, platform
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
server = smtplib.SMTP('smtp.сайт.ru', 25)
sender = 'root@'+platform.node()
to = 'мояпочта@сайт.ru'
ip = 'xxx.xxx.xxx.xxx'
sship = os.environ['SSH_CONNECTION']
loginname = os.environ['LOGNAME']
msg = MIMEMultipart()
msg['Subject'] = '[SSH] Login on ' + platform.node()
msg['From'] = sender
msg['To'] = to
text = 'Remote connection from\t' + sship + '\nLogin ' + loginname
msg.attach (MIMEText(text, 'plain'))
textmail = msg.as_string()
if ip in sship:
print ('hi. Welcome!')
else:
print ('who is it?')
server.sendmail(sender, to, textmail)
#Сохраняем скрипт в файл, например, noticessh.py и прописываем путь к нему в /etc/ssh/sshrc. К сожалению не со всеми версиями #второго питона работает, так же мешает авторизовываться по sftp в случае, если sftp работает через ssh. FileZilla, например, #ругается «Оut of memory!» Надо бы допилить, но на данный момент к сожалению знаний по питону не достаточно.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment