-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyip.sh
More file actions
39 lines (34 loc) · 1.11 KB
/
Copy pathmyip.sh
File metadata and controls
39 lines (34 loc) · 1.11 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
#!/usr/bin/env bash
# myip.sh — 查本机出口 IP / 信息(ipyou.net)
#
# 用法:
# ./myip.sh 本机 IP 信息(纯文本)
# ./myip.sh -q 只输出 IP(适合脚本取值)
# ./myip.sh -j JSON
# ./myip.sh -x socks5h://127.0.0.1:1080 走代理查(确认代理出口)
#
# 依赖: curl(-q 与 -j 均无需 jq)
set -euo pipefail
BASE="${IPYOU_BASE:-https://ipyou.net}"
TIMEOUT="${IPYOU_TIMEOUT:-10}"
mode="text"
proxy=""
usage() { sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//'; exit 0; }
while [ $# -gt 0 ]; do
case "$1" in
-q|--quiet) mode="ip" ;;
-j|--json) mode="json" ;;
-x|--proxy) shift; proxy="${1:-}" ;;
-h|--help) usage ;;
*) echo "未知参数: $1" >&2; exit 2 ;;
esac
shift
done
curl_opts=(-s --max-time "$TIMEOUT")
if [ -n "$proxy" ]; then curl_opts+=(--proxy "$proxy"); fi
case "$mode" in
# 只取 IP:从纯文本首行解析,免 jq 依赖
ip) curl "${curl_opts[@]}" "$BASE/" | sed -n 's/^IP[[:space:]]*:[[:space:]]*//p' | head -1 ;;
json) curl "${curl_opts[@]}" -H "Accept: application/json" "$BASE/" ;;
text) curl "${curl_opts[@]}" "$BASE/" ;;
esac