ssh_tg_alert/ssh_login_info.sh
2025-08-26 15:58:19 +03:00

52 lines
1.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
TOKEN="999999999:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" # Токен бота
CHAT_ID="999999999" # Ваш ID либо ID чата
URL="https://api.telegram.org/bot${TOKEN}/sendMessage?parse_mode=markdown"
DATE="$(date +%d-%m-%Y\ %H:%M:%S)"
TMP_INFO="/tmp/ssh_caption_file.txt"
send_notification() {
local tmp_file="$1"
local country city org
curl -s -m 5 "http://ip-api.com/json/$PAM_RHOST" -o "$tmp_file" >/dev/null 2>&1
country=$(jq -r '.country // empty' "$tmp_file" 2>/dev/null) || country=""
city=$(jq -r '.city // empty' "$tmp_file" 2>/dev/null) || city=""
org=$(jq -r '.as // empty' "$tmp_file" 2>/dev/null) || org=""
if [ -z "$country" ] || [ -z "$city" ] || [ -z "$org" ]; then
TEXT="📡 Новое SSH подключение\n"
TEXT+="⌚ *Время*: \`$DATE\`\n"
TEXT+="*На хост*\n"
TEXT+="🖥 *Host:* \`$HOSTNAME\`\n"
TEXT+="👤 *User:* \`$PAM_USER\`\n"
TEXT+="*С адреса*\n"
TEXT+="🔎 *Source IP:* \`$PAM_RHOST\`"
else
TEXT="📡 Новое SSH подключение\n"
TEXT+="⌚ *Время*: \`$DATE\`\n"
TEXT+="*На хост*\n"
TEXT+="🖥 *Host:* \`$HOSTNAME\`\n"
TEXT+="👤 *User:* \`$PAM_USER\`\n"
TEXT+="*С адреса*\n"
TEXT+="🔎 *Source IP:* \`$PAM_RHOST\`\n"
TEXT+="🌎 *Country:* \`$country\`\n"
TEXT+="🏙 *City:* \`$city\`\n"
TEXT+="🕋 *Organisation:* \`$org\`"
fi
TEXT=$(echo -e "${TEXT}")
curl -s -m 10 -X POST "$URL" \
-d chat_id="$CHAT_ID" \
-d text="$TEXT" \
-d parse_mode=Markdown >/dev/null 2>&1
rm -f "$tmp_file"
}
if [ -n "$PAM_RHOST" ]; then
( sleep 0.1; send_notification "$TMP_INFO" ) &
fi
exit 0