Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # === Список IP-адрес з описом (IP|Опис) ===
- IP_LIST=$(cat <<EOF
- 172.31.0.2|Zhu
- 172.31.0.3|Yaho
- 172.31.0.4|Yaho
- 172.31.0.5|Yaho
- EOF
- )
- BRIDGE="bridge0"
- TAP="tap0"
- OPENVPN_SERVICE="/usr/local/etc/rc.d/openvpn"
- LOG_FILE="/var/log/openvpn_monitor.log"
- MONITOR_LOG="/var/log/openvpn_monitor_status.log"
- # === Telegram Bot ===
- TELEGRAM_BOT_TOKEN=""
- TELEGRAM_CHAT_IDS="123457 9104477"
- # === Поведінка ===
- RESTART_OPENVPN="off" # on/off
- REATTACH_TAP="off" # on/off
- # === Логування з часом ===
- log_with_time() {
- echo "$(date '+%Y-%m-%d %H:%M:%S') $1" | tee -a "$LOG_FILE"
- }
- # === Відправка повідомлення в Telegram ===
- send_telegram_message() {
- local message=$1
- for chat_id in $TELEGRAM_CHAT_IDS; do
- curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
- -d chat_id="$chat_id" \
- -d text="$message" > /dev/null
- done
- }
- # === Відновлення VPN-з'єднання ===
- restore_connection() {
- log_with_time "[INFO] Виконується відновлення зв’язку..."
- if [ "$REATTACH_TAP" = "on" ]; then
- ifconfig "$BRIDGE" deletem "$TAP" 2>/dev/null && \
- log_with_time "[INFO] $TAP видалено з $BRIDGE." || \
- log_with_time "[WARNING] $TAP не знайдено або вже видалено."
- fi
- if [ "$RESTART_OPENVPN" = "on" ]; then
- log_with_time "[INFO] Перезапуск OpenVPN..."
- service openvpn restart
- fi
- if [ "$REATTACH_TAP" = "on" ]; then
- log_with_time "[INFO] Додаємо $TAP назад у $BRIDGE..."
- retry=0
- while ! ifconfig "$BRIDGE" addm "$TAP" 2>/dev/null; do
- retry=$((retry + 1))
- log_with_time "[ERROR] Не вдалося додати $TAP (спроба $retry)..."
- [ "$retry" -ge 3 ] && log_with_time "[CRITICAL] Перевищено кількість спроб додавання." && break
- sleep 2
- done
- if ifconfig "$BRIDGE" | grep -q "$TAP"; then
- log_with_time "[INFO] $TAP успішно додано у $BRIDGE."
- else
- log_with_time "[ERROR] $TAP не додано у $BRIDGE після відновлення."
- fi
- fi
- }
- # === Додати IP у лог недоступних ===
- log_ip_down() {
- local ip=$1
- grep -q "$ip" "$MONITOR_LOG" || echo "$ip" >> "$MONITOR_LOG"
- }
- # === Видалити IP з логу, якщо знову доступний ===
- remove_ip_from_log() {
- local ip=$1
- sed -i '' "/$ip/d" "$MONITOR_LOG"
- }
- # === Перевірка доступності IP ===
- check_ping() {
- local ip=$1
- local desc=$2
- if ping -c 1 -t 2 "$ip" > /dev/null 2>&1; then
- if grep -q "$ip" "$MONITOR_LOG"; then
- remove_ip_from_log "$ip"
- log_with_time "[INFO] $ip ($desc) знову доступний."
- send_telegram_message "✅ $desc ($ip) знову доступний."
- fi
- else
- if ! grep -q "$ip" "$MONITOR_LOG"; then
- log_ip_down "$ip"
- log_with_time "[ERROR] Немає відповіді від $ip ($desc)."
- send_telegram_message "⚠️ $desc ($ip) недоступний!"
- restore_connection
- fi
- fi
- }
- # === Повторна перевірка IP з лог-файлу ===
- check_ips_from_log() {
- [ -f "$MONITOR_LOG" ] || return
- while IFS='|' read -r ip desc; do
- grep -q "$ip" "$MONITOR_LOG" || continue
- [ -z "$desc" ] && desc="Невідомий пристрій"
- log_with_time "[INFO] Перевірка з логу: $ip ($desc)"
- check_ping "$ip" "$desc"
- done <<EOF
- $IP_LIST
- EOF
- }
- # === Ініціалізація лог-файлу ===
- check_log_file() {
- [ -f "$MONITOR_LOG" ] || { touch "$MONITOR_LOG"; chmod 666 "$MONITOR_LOG"; }
- }
- # === Головний запуск ===
- check_log_file
- # Основна перевірка IP
- while IFS='|' read -r ip desc; do
- check_ping "$ip" "$desc"
- done <<EOF
- $IP_LIST
- EOF
- # Перевірка раніше недоступних
- check_ips_from_log
- exit 0
Advertisement
Comments
-
- /sbin/ifconfig bridge0 addm tap0
- @reboot sleep 30 && /bin/bridge.sh
Add Comment
Please, Sign In to add comment
Advertisement