feat: scan-ip support port

This commit is contained in:
2026-05-25 21:36:14 +08:00
parent d82c9af650
commit 137e67503a
2 changed files with 98 additions and 18 deletions
+6 -4
View File
@@ -7,9 +7,11 @@ font-size = 16
font-feature = -calt # 如果你不喜欢连字(Ligatures)可以禁用 font-feature = -calt # 如果你不喜欢连字(Ligatures)可以禁用
# 窗口样式:隐藏标题栏,让视觉更纯粹 # 窗口样式:隐藏标题栏,让视觉更纯粹
window-decoration = true #window-decoration = true
macos-titlebar-style = transparent #macos-titlebar-style = transparent
macos-titlebar-style = tabs
window-title-font-family = "FiraCode Nerd Font" window-title-font-family = "FiraCode Nerd Font"
#fullscreen=non-native
# 背景透明度与模糊 (适合 2017 MBP 的精致感) # 背景透明度与模糊 (适合 2017 MBP 的精致感)
background-opacity = 0.95 background-opacity = 0.95
@@ -39,11 +41,11 @@ scrollback-limit = 10000
# Ghostty 配置 # Ghostty 配置
desktop-notifications = true desktop-notifications = true
#term = xterm-256color term = xterm-256color
scrollbar = system scrollbar = system
mouse-shift-capture = true mouse-shift-capture = true
shell-integration-features = ssh-env,ssh-terminfo #shell-integration-features = ssh-env,ssh-terminfo
# 开启所有 shell 集成特性,包含 ssh-terminfo 自动注入 # 开启所有 shell 集成特性,包含 ssh-terminfo 自动注入
shell-integration = fish shell-integration = fish
Regular → Executable
+84 -6
View File
@@ -1,5 +1,15 @@
#!/bin/bash #!/bin/bash
# 用法:
# ./scan-ip.sh # ICMP 扫描本网段在线 IP
# ./scan-ip.sh 22 # 扫描本网段中 22 端口开放的 IP
# ./scan-ip.sh 80,443,8080 # 扫描本网段中任一指定端口开放的 IP
# ./scan-ip.sh 22 10.0.0 # 指定网段前缀(不写则使用本机所在网段)
PORT_SPEC="$1"
PREFIX_ARG="$2"
TIMEOUT=1
if command -v ip &>/dev/null; then if command -v ip &>/dev/null; then
LOCAL_IP=$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1) LOCAL_IP=$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1)
elif command -v ifconfig &>/dev/null; then elif command -v ifconfig &>/dev/null; then
@@ -10,23 +20,91 @@ fi
[ -z "$LOCAL_IP" ] && echo "错误: 未能检测到本地 IP" && exit 1 [ -z "$LOCAL_IP" ] && echo "错误: 未能检测到本地 IP" && exit 1
PREFIX="${LOCAL_IP%.*}" if [ -n "$PREFIX_ARG" ]; then
PREFIX="$PREFIX_ARG"
else
PREFIX="${LOCAL_IP%.*}"
fi
# 解析端口参数 -> PORTS 数组
PORTS=()
if [ -n "$PORT_SPEC" ]; then
if [[ "$PORT_SPEC" =~ ^[0-9]+$ ]]; then
PORTS=("$PORT_SPEC")
elif [[ "$PORT_SPEC" =~ ^[0-9]+-[0-9]+$ ]]; then
START_PORT=${PORT_SPEC%-*}
END_PORT=${PORT_SPEC#*-}
if [ "$START_PORT" -gt "$END_PORT" ]; then
echo "错误: 起始端口 ($START_PORT) 不能大于结束端口 ($END_PORT)" && exit 1
fi
for p in $(seq "$START_PORT" "$END_PORT"); do PORTS+=("$p"); done
elif [[ "$PORT_SPEC" =~ ^[0-9,]+$ ]]; then
IFS=',' read -ra PORTS <<<"$PORT_SPEC"
else
echo "错误: 无效的端口格式: $PORT_SPEC"
echo "支持: 单端口(22) / 列表(80,443) / 范围(8000-8100)"
exit 1
fi
fi
echo "本机 IP: $LOCAL_IP,扫描网段: $PREFIX.1-254" echo "本机 IP: $LOCAL_IP,扫描网段: $PREFIX.1-254"
if [ ${#PORTS[@]} -gt 0 ]; then
echo "扫描端口: ${PORTS[*]} (TCP,超时 ${TIMEOUT}s)"
else
echo "扫描模式: ICMP (在线检测)"
fi
echo "------------------------------------------------------" echo "------------------------------------------------------"
# macOS: -W 单位毫秒,-S 绑定源 IP # ---------- 模式一:ICMP 在线扫描(无端口参数) ----------
# Linux: -W 单位秒, -I 绑定源 IP if [ ${#PORTS[@]} -eq 0 ]; then
# 绑定源 IP 是为了防止 VPN 劫持路由导致 ping 走错网卡、ARP 不触发 # macOS: -W 单位毫秒,-S 绑定源 IP
if [ "$(uname -s)" = "Darwin" ]; then # Linux: -W 单位秒, -I 绑定源 IP
if [ "$(uname -s)" = "Darwin" ]; then
for i in $(seq 1 254); do for i in $(seq 1 254); do
ping -c 1 -W 1000 -S "$LOCAL_IP" "$PREFIX.$i" >/dev/null 2>&1 && echo "$PREFIX.$i 在线" & ping -c 1 -W 1000 -S "$LOCAL_IP" "$PREFIX.$i" >/dev/null 2>&1 && echo "$PREFIX.$i 在线" &
done done
else else
for i in $(seq 1 254); do for i in $(seq 1 254); do
ping -c 1 -W 1 -I "$LOCAL_IP" "$PREFIX.$i" >/dev/null 2>&1 && echo "$PREFIX.$i 在线" & ping -c 1 -W 1 -I "$LOCAL_IP" "$PREFIX.$i" >/dev/null 2>&1 && echo "$PREFIX.$i 在线" &
done done
fi
wait
echo "------------------------------------------------------"
echo "扫描完成"
exit 0
fi fi
# ---------- 模式二:TCP 端口扫描 ----------
if ! command -v nc &>/dev/null; then
echo "错误: 找不到 nc (netcat),请先安装:"
echo " macOS: brew install netcat"
echo " Debian/Ubuntu: sudo apt-get install netcat-openbsd"
exit 1
fi
# nc 在不同实现里 -z/-w 行为基本一致;用 -G 在 macOS 上控制连接超时更准
NC_OPTS=(-z -w "$TIMEOUT")
if [ "$(uname -s)" = "Darwin" ]; then
NC_OPTS+=(-G "$TIMEOUT")
fi
check_host() {
local ip="$1"
local hit=()
for port in "${PORTS[@]}"; do
if nc "${NC_OPTS[@]}" "$ip" "$port" >/dev/null 2>&1; then
hit+=("$port")
fi
done
if [ ${#hit[@]} -gt 0 ]; then
echo "$ip 开放: ${hit[*]}"
fi
}
for i in $(seq 1 254); do
check_host "$PREFIX.$i" &
done
wait wait
echo "------------------------------------------------------" echo "------------------------------------------------------"
echo "扫描完成" echo "扫描完成"