-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipwatch.sh
More file actions
62 lines (54 loc) · 1.76 KB
/
Copy pathipwatch.sh
File metadata and controls
62 lines (54 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# ipwatch.sh — 持续监控出口 IP,只在**变化时**输出(ipyou.net)
#
# 适合: 换代理节点、拨号重连、机场线路漂移、判断代理是否掉线回落到真实 IP
#
# 用法:
# ./ipwatch.sh 每 10 秒查一次
# ./ipwatch.sh 30 每 30 秒查一次
# ./ipwatch.sh 10 -d 变化时同时打印完整详情(归属/类型/风控)
# ./ipwatch.sh 10 -x socks5h://127.0.0.1:1080 盯代理出口
#
# 依赖: curl。Ctrl-C 退出。
set -uo pipefail
BASE="${IPYOU_BASE:-https://ipyou.net}"
TIMEOUT="${IPYOU_TIMEOUT:-10}"
interval=10
detail=0
proxy=""
usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; exit 0; }
while [ $# -gt 0 ]; do
case "$1" in
-d|--detail) detail=1 ;;
-x|--proxy) shift; proxy="${1:-}" ;;
-h|--help) usage ;;
''|*[!0-9]*) echo "未知参数: $1" >&2; exit 2 ;;
*) interval="$1" ;;
esac
shift
done
curl_opts=(-s --max-time "$TIMEOUT")
if [ -n "$proxy" ]; then curl_opts+=(--proxy "$proxy"); fi
last=""
echo "监控出口 IP,每 ${interval}s 检查一次(Ctrl-C 退出)"
while true; do
# 从纯文本首行解析 IP,免 jq 依赖
now=$(curl "${curl_opts[@]}" "$BASE/" 2>/dev/null \
| sed -n 's/^IP[[:space:]]*:[[:space:]]*//p' | head -1 | tr -d '\r')
ts=$(date '+%F %T')
if [ -z "$now" ]; then
[ "$last" != "__down__" ] && echo "[$ts] ⚠ 查询失败(网络不通或超时)"
last="__down__"
elif [ "$now" != "$last" ]; then
if [ -z "$last" ] || [ "$last" = "__down__" ]; then
echo "[$ts] 当前出口: $now"
else
echo "[$ts] ⚡ IP 变化: $last → $now"
fi
if [ "$detail" = "1" ]; then
curl "${curl_opts[@]}" "$BASE/" | sed 's/^/ /'
fi
last="$now"
fi
sleep "$interval"
done