From a3769959474f9fa81f5a4a9a78db5e2741a85552 Mon Sep 17 00:00:00 2001 From: laily Date: Tue, 14 Jul 2026 21:45:02 +0800 Subject: [PATCH] feat: update ghostty ssh term --- ghostty/config.ghostty | 2 +- script/dirsize.sh | 6 +- script/scan-ip.sh | 69 ++++++++++++++++++++-- script/scan-port.sh | 127 ----------------------------------------- 4 files changed, 68 insertions(+), 136 deletions(-) delete mode 100755 script/scan-port.sh diff --git a/ghostty/config.ghostty b/ghostty/config.ghostty index 4f390e2..a9a6293 100644 --- a/ghostty/config.ghostty +++ b/ghostty/config.ghostty @@ -46,6 +46,6 @@ term = xterm-256color scrollbar = system mouse-shift-capture = true -#shell-integration-features = ssh-env,ssh-terminfo +shell-integration-features = ssh-env # 开启所有 shell 集成特性,包含 ssh-terminfo 自动注入 shell-integration = fish diff --git a/script/dirsize.sh b/script/dirsize.sh index 33c8d1d..f14f388 100755 --- a/script/dirsize.sh +++ b/script/dirsize.sh @@ -80,15 +80,15 @@ fi list_dir_sizes() { if [[ "$OSTYPE" == "darwin"* ]]; then - sudo du -h -d "$DEPTH" "$TARGET_DIR" 2>/dev/null + du -h -d "$DEPTH" "$TARGET_DIR" 2>/dev/null else - sudo du -h --max-depth="$DEPTH" "$TARGET_DIR" 2>/dev/null + du -h --max-depth="$DEPTH" "$TARGET_DIR" 2>/dev/null fi | awk -v dir="$TARGET_DIR" '$2 != dir {print}' } # 用 ls 读取直接子文件(一次 readdir),避免 find 递归 list_direct_file_sizes() { - sudo ls -lA "$TARGET_DIR" 2>/dev/null | awk -v dir="$TARGET_DIR" ' + ls -lA "$TARGET_DIR" 2>/dev/null | awk -v dir="$TARGET_DIR" ' function human(bytes, units, i, size) { split("B K M G T", units, " ") i = 1 diff --git a/script/scan-ip.sh b/script/scan-ip.sh index 684813b..bbf96d8 100755 --- a/script/scan-ip.sh +++ b/script/scan-ip.sh @@ -1,5 +1,5 @@ #!/bin/bash -# ICMP/TCP 扫描本网段或指定网段的在线主机与开放端口 +# ICMP/TCP 扫描本网段或指定网段的在线主机、主机名与开放端口 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck disable=SC1091 @@ -10,7 +10,7 @@ usage() { 用法: scan-ip [端口或网段参数...] 扫描局域网主机: - 无参数 ICMP 扫描本网段 .1-.254 + 无参数 ICMP 扫描本网段 .1-.254,并尽力显示主机名 10.0.0 扫描指定网段前缀 22 扫描本网段 TCP 22 端口 80,443,8080 扫描多个端口(任一开放即显示) @@ -20,6 +20,7 @@ usage() { -h, --help 显示此帮助 依赖: ping;端口模式需要 nc (netcat)。 + 主机名: macOS 使用系统解析器;Linux 使用 NSS(getent),安装 avahi-utils 时也会查询 mDNS;均会回退反向 DNS。 EOF } @@ -95,14 +96,72 @@ else fi echo "------------------------------------------------------" +hostname_for_ip() { + local ip="$1" + local hostname="" + + # macOS 的系统解析器包含本机 hosts、DNS 和可用的 Bonjour/mDNS 记录。 + if [ "$(uname -s)" = "Darwin" ] && command -v dscacheutil &>/dev/null; then + hostname=$(dscacheutil -q host -a ip_address "$ip" 2>/dev/null \ + | awk -F': ' '/^name: / {print $2; exit}') + fi + + # Linux 的 NSS 配置可包含 /etc/hosts、DNS 与 mDNS(例如 libnss-mdns)。 + if [ -z "$hostname" ] && command -v getent &>/dev/null; then + hostname=$(getent hosts "$ip" 2>/dev/null | awk 'NR == 1 {print $2}') + fi + + # 未配置 NSS mDNS 时,avahi-utils 仍可直接查询局域网的 .local 名称。 + if [ -z "$hostname" ] && command -v avahi-resolve-address &>/dev/null; then + if command -v timeout &>/dev/null; then + hostname=$(timeout 1 avahi-resolve-address -4 "$ip" 2>/dev/null \ + | awk -F '\t' 'NR == 1 {print $2}') + else + hostname=$(avahi-resolve-address -4 "$ip" 2>/dev/null \ + | awk -F '\t' 'NR == 1 {print $2}') + fi + fi + + # 没有本地记录时再查询 PTR;限制超时,避免名称解析拖慢扫描。 + if [ -z "$hostname" ] && command -v dig &>/dev/null; then + # macOS 的 dig 会在 DNS 超时时将 ";; connection timed out" 输出到 stdout。 + # PTR 记录只接受由主机名字符组成的一行,避免将诊断信息显示为主机名。 + hostname=$(dig +time=1 +tries=1 +short -x "$ip" 2>/dev/null \ + | awk '/^[[:alnum:]_][[:alnum:]_.-]*\.?$/ {print; exit}') + fi + + # PTR 记录通常以点结尾,显示时去掉以保持紧凑。 + printf '%s' "${hostname%.}" +} + +format_host() { + local ip="$1" + local hostname + hostname=$(hostname_for_ip "$ip") + + if [ -n "$hostname" ]; then + printf '%s (%s)' "$ip" "$hostname" + else + printf '%s' "$ip" + fi +} + if [ ${#PORTS[@]} -eq 0 ]; then if [ "$(uname -s)" = "Darwin" ]; then for i in $(seq 1 254); do - ping -c 1 -W 1000 -S "$LOCAL_IP" "$PREFIX.$i" >/dev/null 2>&1 && echo "$PREFIX.$i 在线" & + ( + ip="$PREFIX.$i" + ping -c 1 -W 1000 -S "$LOCAL_IP" "$ip" >/dev/null 2>&1 \ + && echo "$(format_host "$ip") 在线" + ) & done else for i in $(seq 1 254); do - ping -c 1 -W 1 -I "$LOCAL_IP" "$PREFIX.$i" >/dev/null 2>&1 && echo "$PREFIX.$i 在线" & + ( + ip="$PREFIX.$i" + ping -c 1 -W 1 -I "$LOCAL_IP" "$ip" >/dev/null 2>&1 \ + && echo "$(format_host "$ip") 在线" + ) & done fi wait @@ -130,7 +189,7 @@ check_host() { fi done if [ ${#hit[@]} -gt 0 ]; then - echo "$ip 开放: ${hit[*]}" + echo "$(format_host "$ip") 开放: ${hit[*]}" fi } diff --git a/script/scan-port.sh b/script/scan-port.sh deleted file mode 100755 index 875122a..0000000 --- a/script/scan-port.sh +++ /dev/null @@ -1,127 +0,0 @@ -#!/bin/bash -# 对指定 IP 进行 TCP 端口扫描 - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck disable=SC1091 -source "$SCRIPT_DIR/common/help.sh" - -usage() { - cat <<'EOF' -用法: scan-port [端口范围] - -扫描目标 IP 的 TCP 端口(使用 nc)。 - -参数: - IP 目标 IPv4 地址(必填) - 端口范围 可选。默认扫描常用端口列表。 - 支持: 80,443,8080 或 1-1000 - -选项: - -h, --help 显示此帮助 - -示例: - scan-port 192.168.1.1 - scan-port 192.168.1.1 22,80,443 - scan-port 192.168.1.1 1-1000 -EOF -} - -dotfile_help_requested "${1:-}" && dotfile_show_help - -if [ -z "$1" ]; then - echo "错误: 请提供要扫描的 IP 地址" >&2 - usage - exit 1 -fi - -TARGET_IP="$1" -TIMEOUT=1 - -if ! [[ "$TARGET_IP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - echo "错误: 无效的 IP 地址格式: $TARGET_IP" - exit 1 -fi - -echo "==========================================" -echo "正在扫描 IP: $TARGET_IP" -echo "==========================================" - -if ! command -v nc &>/dev/null; then - echo "错误: 找不到 nc (netcat) 命令。" - exit 1 -fi - -if [ -z "$2" ]; then - PORTS=(21 22 23 25 53 80 110 111 135 139 143 443 445 993 995 1723 3306 3389 5432 5900 8080 8443) - echo "使用默认常用端口列表" -else - PORT_SPEC="$2" - - if [[ "$PORT_SPEC" =~ ^[0-9]+-[0-9]+$ ]]; then - START_PORT=$(echo "$PORT_SPEC" | cut -d'-' -f1) - END_PORT=$(echo "$PORT_SPEC" | cut -d'-' -f2) - - if [ "$START_PORT" -gt "$END_PORT" ]; then - echo "错误: 起始端口 ($START_PORT) 不能大于结束端口 ($END_PORT)" - exit 1 - fi - - PORTS=($(seq "$START_PORT" "$END_PORT")) - echo "扫描端口范围: $START_PORT-$END_PORT" - elif [[ "$PORT_SPEC" =~ ^[0-9,]+$ ]]; then - IFS=',' read -ra PORTS <<<"$PORT_SPEC" - echo "扫描指定端口: $PORT_SPEC" - else - echo "错误: 无效的端口格式: $PORT_SPEC" - exit 1 - fi -fi - -echo "超时设置: ${TIMEOUT}秒" -echo "------------------------------------------------------" - -OPEN_PORTS=() -CLOSED_COUNT=0 -TOTAL_PORTS=${#PORTS[@]} - -scan_port() { - local port=$1 - local ip=$2 - - if nc -z -v -w "$TIMEOUT" "$ip" "$port" 2>&1 | grep -q "succeeded\|open"; then - echo "✅ 端口 $port: 开放" - return 0 - else - return 1 - fi -} - -for port in "${PORTS[@]}"; do - if scan_port "$port" "$TARGET_IP"; then - OPEN_PORTS+=("$port") - else - ((CLOSED_COUNT++)) - fi -done - -wait - -echo "------------------------------------------------------" -echo "扫描完成!" -echo "==========================================" -echo "总计扫描端口数: $TOTAL_PORTS" -echo "开放端口数: ${#OPEN_PORTS[@]}" -echo "关闭端口数: $CLOSED_COUNT" - -if [ ${#OPEN_PORTS[@]} -gt 0 ]; then - echo "" - echo "开放的端口列表:" - for port in "${OPEN_PORTS[@]}"; do - echo " - $port" - done -else - echo "" - echo "未发现开放的端口" -fi - -echo "=========================================="