Last active
March 26, 2025 10:31
-
-
Save Ansen/6cb75b904fcb83c7ca002a0db9c5f265 to your computer and use it in GitHub Desktop.
check_zerotier.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# 默认参数 | |
DEFAULT_IP="10.0.0.1" | |
DEFAULT_COUNT=3 | |
DEFAULT_TIMEOUT=2 | |
DEFAULT_INTERFACE="ztukuvylmh" | |
INTERVAL=60 | |
# 函数:检查 IP 格式 | |
validate_ip() { | |
local ip="$1" | |
if echo "$ip" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' > /dev/null; then | |
return 0 | |
else | |
echo "Invalid IP address format: $ip" | |
return 1 | |
fi | |
} | |
# 函数:检查接口是否存在 | |
validate_interface() { | |
local iface="$1" | |
if ip link show "$iface" > /dev/null 2>&1; then | |
return 0 | |
else | |
echo "Interface $iface does not exist" | |
return 1 | |
fi | |
} | |
# 函数:检查连通性 | |
check_connectivity() { | |
local ip="$1" | |
local count="$2" | |
local timeout="$3" | |
local iface="$4" | |
ping -c "$count" -W "$timeout" -I "$iface" "$ip" > /dev/null 2>&1 | |
return $? | |
} | |
# 函数:重启 ZeroTier | |
restart_zerotier() { | |
echo "$(date '+%Y-%m-%d %H:%M:%S') - Cannot reach $TARGET_IP via $INTERFACE, restarting ZeroTier..." | |
/etc/init.d/zerotier restart | |
sleep 10 | |
} | |
# 函数:主逻辑 | |
main_loop() { | |
local ip="$1" | |
local count="$2" | |
local timeout="$3" | |
local iface="$4" | |
echo "Starting IP connectivity check for $ip via interface $iface..." | |
echo "Checking every $INTERVAL seconds with $count attempts and $timeout seconds timeout" | |
while true; do | |
if ! check_connectivity "$ip" "$count" "$timeout" "$iface"; then | |
restart_zerotier | |
else | |
echo "$(date '+%Y-%m-%d %H:%M:%S') - $ip is reachable via $iface" | |
fi | |
sleep "$INTERVAL" | |
done | |
} | |
# 解析命令行参数 | |
TARGET_IP="$DEFAULT_IP" | |
PING_COUNT="$DEFAULT_COUNT" | |
PING_TIMEOUT="$DEFAULT_TIMEOUT" | |
INTERFACE="$DEFAULT_INTERFACE" | |
while [ "$#" -gt 0 ]; do | |
case "$1" in | |
--ip) | |
TARGET_IP="$2" | |
shift 2 | |
;; | |
-c|--count) | |
PING_COUNT="$2" | |
shift 2 | |
;; | |
-t|--timeout) | |
PING_TIMEOUT="$2" | |
shift 2 | |
;; | |
--interface) | |
INTERFACE="$2" | |
shift 2 | |
;; | |
*) | |
echo "Usage: $0 [--ip <target_ip>] [-c|--count <ping_count>] [-t|--timeout <timeout_seconds>] [--interface <interface>]" | |
echo "Example: $0 --ip 192.168.1.1 -c 5 -t 3 --interface ztukuvylmh" | |
exit 1 | |
;; | |
esac | |
done | |
# 验证参数 | |
if ! validate_ip "$TARGET_IP"; then | |
exit 1 | |
fi | |
if ! echo "$PING_COUNT" | grep -E '^[0-9]+$' > /dev/null || [ "$PING_COUNT" -lt 1 ]; then | |
echo "Invalid ping count: $PING_COUNT (must be a positive integer)" | |
exit 1 | |
fi | |
if ! echo "$PING_TIMEOUT" | grep -E '^[0-9]+$' > /dev/null || [ "$PING_TIMEOUT" -lt 1 ]; then | |
echo "Invalid timeout: $PING_TIMEOUT (must be a positive integer)" | |
exit 1 | |
fi | |
if ! validate_interface "$INTERFACE"; then | |
echo "Warning: Interface $INTERFACE not found, proceeding anyway (it might appear after ZeroTier restarts)" | |
fi | |
# 运行主逻辑 | |
main_loop "$TARGET_IP" "$PING_COUNT" "$PING_TIMEOUT" "$INTERFACE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment