用户工具

站点工具


homelab

Homelab

HomeLab IP 提醒脚本

Bash
  1. #!/bin/bash
  2.  
  3. LOG_FILE="/tmp/notify-ip.log"
  4. BARK_TITLE="北京 daheng 内网 IP 报告"
  5. BARK_GROUP="Server IP 提醒"
  6. RETRY_COUNT=0
  7. MAX_RETRIES=60
  8. BARK_PUSH_URL=""
  9. BARK_DEVICE_KEY=""
  10.  
  11. log() {
  12. local level="$1"
  13. shift
  14. printf '%s [%s] %s\n' "$(date '+%Y-%m-%dT%H:%M:%S%z')" "$level" "$*" >> "$LOG_FILE"
  15. }
  16.  
  17. MISSING_CONFIG=()
  18. if [ -z "${BARK_PUSH_URL:-}" ]; then
  19. MISSING_CONFIG+=("BARK_PUSH_URL")
  20. fi
  21. if [ -z "${BARK_DEVICE_KEY:-}" ]; then
  22. MISSING_CONFIG+=("BARK_DEVICE_KEY")
  23. fi
  24. if [ "${#MISSING_CONFIG[@]}" -gt 0 ]; then
  25. log ERROR "Missing required Bark config: ${MISSING_CONFIG[*]}"
  26. exit 1
  27. fi
  28.  
  29. if ! command -v python3 > /dev/null 2>&1; then
  30. log ERROR "python3 is required to build the Bark payload"
  31. exit 1
  32. fi
  33.  
  34. if ! command -v curl > /dev/null 2>&1; then
  35. log ERROR "curl is required to send the Bark notification"
  36. exit 1
  37. fi
  38.  
  39. log INFO "Waiting for network connectivity; max retries: ${MAX_RETRIES}"
  40. while ! ping -c 1 -W 1 8.8.8.8 > /dev/null 2>&1; do
  41. RETRY_COUNT=$((RETRY_COUNT + 1))
  42. if [ "$RETRY_COUNT" -ge "$MAX_RETRIES" ]; then
  43. log ERROR "Network timeout after ${MAX_RETRIES} attempts, exiting"
  44. exit 1
  45. fi
  46.  
  47. if [ $((RETRY_COUNT % 10)) -eq 0 ]; then
  48. log INFO "Network still unavailable after ${RETRY_COUNT} attempts"
  49. fi
  50.  
  51. sleep 1
  52. done
  53. log INFO "Network connectivity confirmed after ${RETRY_COUNT} attempts"
  54.  
  55. INTERNAL_IPS=$(ip -4 addr show | awk '/ inet / { split($2, ip, "/"); if (ip[1] !~ /^127\./) print ip[1] }')
  56.  
  57. if [ -z "$INTERNAL_IPS" ]; then
  58. MESSAGE="无法获取内网 IP 地址。"
  59. else
  60. MESSAGE="主机内网 IP 地址:$(printf '%s\n' "$INTERNAL_IPS" | tr '\n' ' ')"
  61. fi
  62.  
  63. JSON_PAYLOAD=$(
  64. BARK_TITLE="$BARK_TITLE" \
  65. BARK_BODY="$MESSAGE" \
  66. BARK_DEVICE_KEY="$BARK_DEVICE_KEY" \
  67. BARK_GROUP="$BARK_GROUP" \
  68. python3 - <<'PY'
  69. import json
  70. import os
  71.  
  72. payload = {
  73. "title": os.environ["BARK_TITLE"],
  74. "body": os.environ["BARK_BODY"],
  75. "device_key": os.environ["BARK_DEVICE_KEY"],
  76. "group": os.environ["BARK_GROUP"],
  77. }
  78.  
  79. print(json.dumps(payload, ensure_ascii=False))
  80. PY
  81. )
  82.  
  83. RESPONSE_FILE=$(mktemp)
  84. log INFO "Sending Bark notification"
  85. HTTP_STATUS=$(
  86. curl -sS -o "$RESPONSE_FILE" -w '%{http_code}' \
  87. -X POST "$BARK_PUSH_URL" \
  88. -H 'Content-Type: application/json; charset=utf-8' \
  89. --data-binary "$JSON_PAYLOAD" 2>> "$LOG_FILE"
  90. )
  91. CURL_EXIT=$?
  92. RESPONSE_BODY=$(cat "$RESPONSE_FILE")
  93. rm -f "$RESPONSE_FILE"
  94.  
  95. log INFO "Bark HTTP status: ${HTTP_STATUS:-unknown}"
  96. log INFO "Bark response body: ${RESPONSE_BODY:-<empty>}"
  97.  
  98. if [ "$CURL_EXIT" -ne 0 ]; then
  99. log ERROR "Bark notification failed with curl exit code ${CURL_EXIT}"
  100. exit "$CURL_EXIT"
  101. fi
  102.  
  103. case "$HTTP_STATUS" in
  104. 2*)
  105. log INFO "Bark notification sent successfully"
  106. ;;
  107. *)
  108. log ERROR "Bark notification returned non-success HTTP status: ${HTTP_STATUS:-unknown}"
  109. exit 1
  110. ;;
  111. esac
  112.  
  113. echo "Attempted to send Bark notification."
/storage/data/pages/homelab.txt · 最后更改: bestony

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki