|
@@ -0,0 +1,59 @@
|
|
|
|
|
+#!/usr/bin/env bash
|
|
|
|
|
+set -uo pipefail
|
|
|
|
|
+
|
|
|
|
|
+PATH="/usr/bin:/bin:/usr/sbin:/sbin"
|
|
|
|
|
+
|
|
|
|
|
+LOG="${VPN_KEEPALIVE_LOG:-/tmp/vpn-keepalive-hxcs-2min.log}"
|
|
|
|
|
+TARGETS="${VPN_KEEPALIVE_TARGETS:-10.0.42.6:3306 10.0.42.49:22}"
|
|
|
|
|
+ATrust_APP_NAME="${VPN_KEEPALIVE_APP_NAME:-aTrust}"
|
|
|
|
|
+
|
|
|
|
|
+mkdir -p "$(dirname "${LOG}")"
|
|
|
|
|
+
|
|
|
|
|
+log() {
|
|
|
|
|
+ printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "${LOG}"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+ensure_atrust_running() {
|
|
|
|
|
+ if ! pgrep -x "${ATrust_APP_NAME}" >/dev/null 2>&1; then
|
|
|
|
|
+ log "aTrust 未运行,尝试启动"
|
|
|
|
|
+ open -ga "${ATrust_APP_NAME}" >/dev/null 2>&1 || true
|
|
|
|
|
+ fi
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+probe_target() {
|
|
|
|
|
+ local host="$1"
|
|
|
|
|
+ local port="$2"
|
|
|
|
|
+
|
|
|
|
|
+ if nc -z -G 3 "${host}" "${port}" >/dev/null 2>&1; then
|
|
|
|
|
+ log "OK ${host}:${port}"
|
|
|
|
|
+ return 0
|
|
|
|
|
+ fi
|
|
|
|
|
+
|
|
|
|
|
+ log "FAIL ${host}:${port}"
|
|
|
|
|
+ return 1
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+main() {
|
|
|
|
|
+ local success=0
|
|
|
|
|
+ local target host port
|
|
|
|
|
+
|
|
|
|
|
+ log "开始 2 分钟 VPN 保活探测"
|
|
|
|
|
+ ensure_atrust_running
|
|
|
|
|
+
|
|
|
|
|
+ read -r -a targets_array <<< "${TARGETS}"
|
|
|
|
|
+ for target in "${targets_array[@]}"; do
|
|
|
|
|
+ host="${target%:*}"
|
|
|
|
|
+ port="${target##*:}"
|
|
|
|
|
+ if probe_target "${host}" "${port}"; then
|
|
|
|
|
+ success=1
|
|
|
|
|
+ fi
|
|
|
|
|
+ done
|
|
|
|
|
+
|
|
|
|
|
+ if [[ "${success}" -eq 0 ]]; then
|
|
|
|
|
+ log "本轮未探测到可达内网地址"
|
|
|
|
|
+ fi
|
|
|
|
|
+
|
|
|
|
|
+ log "本轮探测结束"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+main "$@"
|