O
O
ostiwe2020-08-14 10:40:52
linux
ostiwe, 2020-08-14 10:40:52

Action on the server when connecting via ssh?

Good afternoon, I would like to try to make it so that when I successfully connect to the server (ssh, ftp), I receive a letter in the mail. Is it real to do? If yes, can you give me some hints or examples how to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
brar, 2020-08-14
@brar

Try your cart bot. Notifications are an order of magnitude more convenient and faster than mail.
I will describe for debian. (for other distros it won't be much different). If not you, then maybe someone else will need everything ready:
Create a file /usr/local/bin/telegram_alert.sh with the following content:

#!/bin/bash
KEY="АПИ_КЛЮЧ:ВАШЕГО_БОТА"
URL="https://api.telegram.org/bot$KEY/sendMessage"
TARGET="айдишник_контакта_или_группы_куда_слать_уведомления"
DATE1="$(date "+%H:%M:%S")"
DATE2="$(date "+%d %B %Y")"
GEO="$(curl ipinfo.io/$PAM_RHOST)"
TEXT="*$PAM_USER* залогинился на *$HOSTNAME* 
Время: $DATE1
Дата: $DATE2
Адрес: $PAM_RHOST
Service: $PAM_SERVICE
TTY: $PAM_TTY
GEO: ${GEO}"
PAYLOAD="chat_id=$TARGET&text=$TEXT&parse_mode=Markdown&disable_web_page_preview=true"
curl -s --max-time 10 --retry 5 --retry-delay 2 --retry-max-time 10 -d "$PAYLOAD" $URL > /dev/null 2>&1 &

In the file /etc/pam.d/sshd add/edit the line:
session optional pam_exec.so type=open_session seteuid /usr/local/bin/telegram_alert.sh

You will receive in this form:
5f36480e9db84988952574.png
How to create your own bot is a separate issue. And it is very light, and there are a lot of instructions on the net.

S
Sanes, 2020-08-14
@Sanes

https://medium.com/@alessandrocuda/ssh-login-alert...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question