Advertisement
skybetik

Checking tunnel status Openvpn client with bridge v2

Jun 22nd, 2025
423
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.25 KB | Fixit | 0 0
  1. #!/bin/sh
  2.  
  3. # === Список IP-адрес з описом (IP|Опис) ===
  4. IP_LIST=$(cat <<EOF
  5. 172.31.0.2|Zhu
  6. 172.31.0.3|Yaho
  7. 172.31.0.4|Yaho
  8. 172.31.0.5|Yaho
  9. EOF
  10. )
  11.  
  12. BRIDGE="bridge0"
  13. TAP="tap0"
  14. OPENVPN_SERVICE="/usr/local/etc/rc.d/openvpn"
  15. LOG_FILE="/var/log/openvpn_monitor.log"
  16. MONITOR_LOG="/var/log/openvpn_monitor_status.log"
  17.  
  18. # === Telegram Bot ===
  19. TELEGRAM_BOT_TOKEN=""
  20. TELEGRAM_CHAT_IDS="123457 9104477"
  21.  
  22. # === Поведінка ===
  23. RESTART_OPENVPN="off"   # on/off
  24. REATTACH_TAP="off"      # on/off
  25.  
  26. # === Логування з часом ===
  27. log_with_time() {
  28.     echo "$(date '+%Y-%m-%d %H:%M:%S') $1" | tee -a "$LOG_FILE"
  29. }
  30.  
  31. # === Відправка повідомлення в Telegram ===
  32. send_telegram_message() {
  33.     local message=$1
  34.     for chat_id in $TELEGRAM_CHAT_IDS; do
  35.         curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
  36.             -d chat_id="$chat_id" \
  37.             -d text="$message" > /dev/null
  38.     done
  39. }
  40.  
  41. # === Відновлення VPN-з'єднання ===
  42. restore_connection() {
  43.     log_with_time "[INFO] Виконується відновлення зв’язку..."
  44.  
  45.     if [ "$REATTACH_TAP" = "on" ]; then
  46.         ifconfig "$BRIDGE" deletem "$TAP" 2>/dev/null && \
  47.         log_with_time "[INFO] $TAP видалено з $BRIDGE." || \
  48.         log_with_time "[WARNING] $TAP не знайдено або вже видалено."
  49.     fi
  50.  
  51.     if [ "$RESTART_OPENVPN" = "on" ]; then
  52.         log_with_time "[INFO] Перезапуск OpenVPN..."
  53.         service openvpn restart
  54.     fi
  55.  
  56.     if [ "$REATTACH_TAP" = "on" ]; then
  57.         log_with_time "[INFO] Додаємо $TAP назад у $BRIDGE..."
  58.         retry=0
  59.         while ! ifconfig "$BRIDGE" addm "$TAP" 2>/dev/null; do
  60.             retry=$((retry + 1))
  61.             log_with_time "[ERROR] Не вдалося додати $TAP (спроба $retry)..."
  62.             [ "$retry" -ge 3 ] && log_with_time "[CRITICAL] Перевищено кількість спроб додавання." && break
  63.             sleep 2
  64.         done
  65.         if ifconfig "$BRIDGE" | grep -q "$TAP"; then
  66.             log_with_time "[INFO] $TAP успішно додано у $BRIDGE."
  67.         else
  68.             log_with_time "[ERROR] $TAP не додано у $BRIDGE після відновлення."
  69.         fi
  70.     fi
  71. }
  72.  
  73. # === Додати IP у лог недоступних ===
  74. log_ip_down() {
  75.     local ip=$1
  76.     grep -q "$ip" "$MONITOR_LOG" || echo "$ip" >> "$MONITOR_LOG"
  77. }
  78.  
  79. # === Видалити IP з логу, якщо знову доступний ===
  80. remove_ip_from_log() {
  81.     local ip=$1
  82.     sed -i '' "/$ip/d" "$MONITOR_LOG"
  83. }
  84.  
  85. # === Перевірка доступності IP ===
  86. check_ping() {
  87.     local ip=$1
  88.     local desc=$2
  89.     if ping -c 1 -t 2 "$ip" > /dev/null 2>&1; then
  90.         if grep -q "$ip" "$MONITOR_LOG"; then
  91.             remove_ip_from_log "$ip"
  92.             log_with_time "[INFO] $ip ($desc) знову доступний."
  93.             send_telegram_message "✅ $desc ($ip) знову доступний."
  94.         fi
  95.     else
  96.         if ! grep -q "$ip" "$MONITOR_LOG"; then
  97.             log_ip_down "$ip"
  98.             log_with_time "[ERROR] Немає відповіді від $ip ($desc)."
  99.             send_telegram_message "⚠️ $desc ($ip) недоступний!"
  100.             restore_connection
  101.         fi
  102.     fi
  103. }
  104.  
  105. # === Повторна перевірка IP з лог-файлу ===
  106. check_ips_from_log() {
  107.     [ -f "$MONITOR_LOG" ] || return
  108.     while IFS='|' read -r ip desc; do
  109.         grep -q "$ip" "$MONITOR_LOG" || continue
  110.         [ -z "$desc" ] && desc="Невідомий пристрій"
  111.         log_with_time "[INFO] Перевірка з логу: $ip ($desc)"
  112.         check_ping "$ip" "$desc"
  113.     done <<EOF
  114. $IP_LIST
  115. EOF
  116. }
  117.  
  118. # === Ініціалізація лог-файлу ===
  119. check_log_file() {
  120.     [ -f "$MONITOR_LOG" ] || { touch "$MONITOR_LOG"; chmod 666 "$MONITOR_LOG"; }
  121. }
  122.  
  123. # === Головний запуск ===
  124.  
  125. check_log_file
  126.  
  127. # Основна перевірка IP
  128. while IFS='|' read -r ip desc; do
  129.     check_ping "$ip" "$desc"
  130. done <<EOF
  131. $IP_LIST
  132. EOF
  133.  
  134. # Перевірка раніше недоступних
  135. check_ips_from_log
  136.  
  137. exit 0
  138.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement