Thông báo khi có user SSH thành công qua email

Khi có user log in vào máy qua SSH, các file trong thư mục /etc/profile.d/ sẽ được thực thi. Lợi dụng việc này, ta sẽ viết script để tự động gửi mail thông báo khi có ai đó log in thành công
Thiết kế:

/opt/batch/backup_web/mail.py   File gửi mail, được viết bằng python

/etc/profile.d/notify_login.sh  File sh, sẽ được chạy mỗi khi login, file này sẽ gọi tới file python ở trên để gửi mail

Nội dung cả 2 file:
File mail.py

#!/usr/bin/python

# -*- coding: utf-8 -*-

# coding=utf-8

import sys

import smtplib

import os

from email.MIMEMultipart import MIMEMultipart

from email.MIMEBase import MIMEBase

from email.MIMEText import MIMEText

from email import Encoders

import datetime

def mail(to='abcd@gmail.com', subject='Email backup du lieu', text='day la body', attach='/var/log/secure'):

  gmail_user='abcd@gmail.com'

  gmail_pwd='ebzrmwozqckeqcoa'

  msg = MIMEMultipart()

  msg['From'] = 'abcd@gmail.com'

  msg['To'] = to

  msg['Subject'] = subject

  msg.attach(MIMEText(text+"\r\n"+datetime.datetime.today().strftime('%Y-%m-%d')))



  part = MIMEBase('application', 'octet-stream')

  part.set_payload(open(attach, 'rb').read())

  Encoders.encode_base64(part)

  part.add_header('Content-Disposition',

          'attachment; filename="%s"' % os.path.basename(attach))

  msg.attach(part)



  mailServer = smtplib.SMTP("smtp.gmail.com", 587)

  mailServer.ehlo()

  mailServer.starttls()

  mailServer.ehlo()

  mailServer.login(gmail_user, gmail_pwd)

  mailServer.sendmail(gmail_user, to, msg.as_string())

  # Should be mailServer.quit(), but that crashes...

  mailServer.close()



mail(subject=sys.argv[1], text=sys.argv[2], attach=sys.argv[3])

File sh

#!/bin/bash



sendmailpy=/opt/batch/backup_web/mail.py

ACCESS_IP=`/bin/echo $SSH_CLIENT | awk '{ print $1 }'`

SUBJECT="$ACCESS_IP Đã truy cập"

BODY="$(date) => $SSH_CLIENT đã truy cập bằng user $USER"

attach_local=/var/log/secure

/bin/nohup /usr/bin/python $sendmailpy $SUBJECT "$BODY" $attach_local >> /var/log/ssh_login_$(date +%F) &

Thông tin về email sử dụng để gửi cảnh báo được định nghĩa trong file mail.py
Lưu ý: Sử dụng trên gmail thấy google chặn đăng nhập đối với các user sử dụng password chính, còn đối với các user bật xác minh 2 bước, sử dụng pass ứng dụng thì google cho đăng nhập thoải mái. các bạn lưu ý.

Simple monitoring – Chương trình monitor mạng đơn giản.

Chương trình được viết bằng python

Hướng dẫn cài đặt:

Cài đặt python3, và các gói cần thiết

yum install epel-release -y & yum install python36 python36-pip.noarch -y && pip3 install requests



Cách chạy app, sau khi thay cấu hình trong file config.py

cd <đường dẫn thư mục app>

python3 monitor_simple.py



Cách thiết lập chạy mỗi phút 1 lần:

crontab -l

Thêm vào dòng sau:

* * * * * /usr/bin/python3 <đường dẫn file monitor_simple.py>

Download:

https://github.com/khachuy2705/monitor_simple

Regular Expressions cơ bản

Cú pháp cơ bản:

.             1 ký tự bất kỳ, ngoại trừ xuống dòng

\.            dấu chấm (\ thể hiện escape)

\d            Một ký tự số

\D            Một ký tự không phải là số

\w            Một chữ hoặc số

\W            Một ký tự không phải là chữ

\s            một Dấu cách

\S            Một ký tự không phải dấu cách

\b            1 từ nguyên vẹn

\n            Xuống dòng

\t            Tab

Modifiers

$             Bắt đầu chuỗi

^             bắt đầu chuỗi

ab|cd         Matches ab hoặc cd.

[ab-d]	      Match a và match các ký tự từ b --> d = bcd

[^ab-d]	      One character except: a, b, c, d

()            Items within parenthesis are retrieved

(a(bc))       Items within the sub-parenthesis are retrieved

Repetitions

[ab]{2}       Exactly 2 continuous occurrences of a or b

[ab]{2,5}     2 to 5 continuous occurrences of a or b

[ab]{2,}      2 or more continuous occurrences of a or b

+             One or more

*             Zero or more

?             0 or 1